【问题标题】:Configure layout in Common.Logging在 Common.Logging 中配置布局
【发布时间】:2014-03-26 16:17:05
【问题描述】:

我目前正在使用 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 文件中添加配置,它就可以工作。但我需要一个基于代码的配置...

【问题讨论】:

    标签: c# log4net appender common.logging


    【解决方案1】:

    您需要在布局和附加程序上调用ActivateOptions

    var layout = new log4net.Layout.PatternLayout()
    {
        ConversionPattern = 
            "%date [%thread] %-5level %logger %ndc - %message%newline"
    };
    
    layout.ActivateOptions();
    appender.Layout = layout;
    

    样本输出:

    2014-03-26 20:29:49,816 [1] DEBUG test logger (null) - log test
    

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多