简体   繁体   中英

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.

Currently my root configuration looks like:

<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

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; 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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