简体   繁体   English

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

[英]Changing the Log4Net Root Level When App is built as Release

I have a project I am working on using log4net, and it works great, but I want to know if i can override the XML configuration for the root "level" attribute for the logging when in debug and release. 我有一个项目,我正在使用log4net,它工作得很好,但我想知道我是否可以在调试和发布时覆盖日志记录的根“级别”属性的XML配置。

Currently my root configuration looks like: 目前我的root配置如下:

<root>
  <level value="WARN"/>
  <appender-ref ref="LogFileAppender"/>
  <appender-ref ref="DebugAppender"/>
</root>

And in my web applications Global.asax class, I was thinking I could wrap something in a 在我的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
}

To change the root logging level to debug when the solution is built in debug. 在调试中构建解决方案时,将根日志记录级别更改为调试。

Is this possible, is my idea a best-practises type soltuion to what I want, and how would I do it (or how would you do it better)? 这是可能的,我的想法是我想要的最佳实践类型解决方案,我将如何做到(或者你会如何做得更好)?

I do the oposite, in debug mode I use the developer configurations which is better for filtering the relevant messages for me; 我做oposite,在调试模式下我使用开发人员配置,这对我来说是更好的过滤相关消息; and in release mode, I lock the level to WARN to prevent a user wants to hack my code (he could use tons of other ways, but this is a 5 seconds trick): 在发布模式下,我将级别锁定为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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM