当App构建为Release时,更改Log4Net根级别

Ash*_*Ash 4 c# asp.net log4net

我有一个项目,我正在使用log4net,它工作得很好,但我想知道我是否可以在调试和发布时覆盖日志记录的根"级别"属性的XML配置.

目前我的root配置如下:

<root>
  <level value="WARN"/>
  <appender-ref ref="LogFileAppender"/>
  <appender-ref ref="DebugAppender"/>
</root>
Run Code Online (Sandbox Code Playgroud)

在我的Web应用程序Global.asax类中,我以为我可以用一些东西包装

protected override void Application_Start(object sender, EventArgs e) {
  base.Application_Start(sender, e);
  XmlConfigurator.Configure();

  #if DEBUG
  //Change logging level to DEBUG
  #endif
}
Run Code Online (Sandbox Code Playgroud)

在调试中构建解决方案时,将根日志记录级别更改为调试.

这是可能的,我的想法是我想要的最佳实践类型解决方案,我将如何做到(或者你会如何做得更好)?

Die*_*cic 6

我做oposite,在调试模式下我使用开发人员配置,这对我来说是更好的过滤相关消息; 在发布模式下,我将级别锁定为WARN以防止用户想要破解我的代码(他可以使用大量其他方法,但这是一个5秒的技巧):

public static void Initialize()
{

#if DEBUG
    ConfigureAndWatch();
#else
    ConfigureAndLockChanges();
#endif

}

#if DEBUG

private static void ConfigureAndWatch()
{
    XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
}

#else

private static void ConfigureAndLockChanges()
{
    // Disable using DEBUG mode in Release mode (to make harder to hack)
    XmlConfigurator.Configure(new FileInfo("log4net.config"));
    foreach (ILoggerRepository repository in LogManager.GetAllRepositories())
    {
        repository.Threshold = Level.Warn;
    }
}

#endif
Run Code Online (Sandbox Code Playgroud)