简体   繁体   English

如何将自定义数据传递到log4net.Core.LoggingEvent对象?

[英]How to pass custom data to the log4net.Core.LoggingEvent object?

I want to get custom data from the log4net.Core.LoggingEvent object inside Append method of my custom log4net appender. 我想从我的自定义log4net附加程序的Append方法内的log4net.Core.LoggingEvent对象获取自定义数据。

public sealed class SimpleAppender : AppenderSkeleton
{
    protected override void Append(LoggingEvent loggingEvent)
    {
        // How can I receive my data?
    }
}

I know that log4net.Core.LoggingEvent contains Properties property. 我知道log4net.Core.LoggingEvent包含Properties属性。 It has type log4net.Util.PropertiesDictionary . 它的类型为log4net.Util.PropertiesDictionary A logger or an appender may attach additional properties to specific events, but I'm not sure that it is what the way I'm looking for. 记录器或附加器可以将附加属性附加到特定事件,但是我不确定这是我所寻找的方式。 I need to get testId here. 我需要在这里获取testId I created that testId in the method marked [TestInitialize] attribute. 我在标记为[TestInitialize]属性的方法中创建了该testId

public class UnitTest1
{
    public TestContext TestContext { get; set; }

    [TestInitialize]
    public void Initialize()
    {
        Guid testId = Guid.NewGuid();
    }

    [TestMethod]
    public void TestMethod1()
    {
        ApplicationLogger.LogInfo("TestMethod1 has started.");
    }       
}

ApplicationLogger class is a simple log4net logger wrapper which prints result into console. ApplicationLogger类是一个简单的log4net记录器包装程序,可将结果打印到控制台中。 I know that I can do something like this. 我知道我可以做这样的事情。

ApplicationLogger.LogInfo("Test message", testId);

public static class ApplicationLogger
{
    private static readonly log4net.ILog log4netLogger;

    public static void LogInfo(Guid testId, string infoMessage)
    {
        ApplicationLogger.log4netLogger.Info(new LogMessageModel
        {
            Text = infoMessage,
            TestId = testId
        });
    }
}

But I don't want to set testId into LogInfo() method every time I need to log something. 但是我不想每次需要记录某些内容时都将testId设置为LogInfo()方法。 Is there another more more elegant way? 还有另一种更优雅的方式吗? Thanks in advance. 提前致谢。

ApplicationLogger添加一个静态的Initialize()函数,该函数接受testId作为参数,然后从UnitTest1.Initialize()调用ApplicationLogger.Initialize(testId) UnitTest1.Initialize()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net Core应用程序中的Log4Net:LoggingEvent设置不正确(LocationInformation属性设置为“?”) - Log4Net in .Net Core application: LoggingEvent not set properly (LocationInformation porperties are set to “?”) 如何将依赖项传递给自定义 .NET Core ILoggerProvider - How to pass dependencies to a custom .NET Core ILoggerProvider 如何在.net核心中记录自定义ado.net SQL查询? - How to log custom ado.net SQL queries in .net core? 如何将自定义对象从异步操作过滤器传递到 ASP.net 核心中的控制器? - How do I pass an custom object from an async action filter to a controller in ASP.net core? 如何在.Net core API中的自定义动作过滤器中通过model - How to pass the model in Custom action filter in .Net core API Nlog with .NET core-如何在没有消息的情况下记录 JSON 对象 - Nlog with .NET core- How to log JSON object without the message 如何通过ASP.NET Core传递自定义JavaScript - How to pass custom JavaScript through ASP.NET Core 如何将对象传递给自定义数据注释验证器 - How to pass a object to a custom Data annotation Validator .Net Core 自定义日志消息格式 - .Net Core custom log-message format 如何在.net Core中的nlog中记录特定的数据和属性 - How to log specific data and properties in nlog in .net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM