[英]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.