我目前正在使用 Common.Logging 和 log4net。我已经实现了一个自定义附加程序。
我正在尝试将下面代码中指定的布局添加到我的日志中。但是当我在自定义附加程序中打印函数RenderLoggingEvent(loggingEvent)
时,我只收到消息(但没有时间戳,...)。
// create properties
// EXTERNAL expects log4net being configured somewhere else in
// your code and does nothing.
NameValueCollection properties = new NameValueCollection();
properties["configType"] = "EXTERNAL";
// set Adapter
Common.Logging.LogManager.Adapter =
new Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(properties);
// create an object of the custom appender
var appender = new SimpleAppender();
appender.Name = "SimpleAppender";
// add layout to the appender
var layout = new log4net.Layout.PatternLayout()
{
ConversionPattern =
"%date [%thread] %-5level %logger %ndc - %message%newline"
};
appender.Layout = layout;
//Let log4net configure itself based on the values provided
appender.ActivateOptions();
log4net.Config.BasicConfigurator.Configure(appender);
如果我在 App.config 文件中添加配置,它就可以工作。但我需要一个基于代码的配置......