views:

472

answers:

1

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)?

+4  A: 

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 look 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
Diego Jancic
Hi Diego, can provide an example for scenarios where users hack your code?
Ricky
Hi Ricky, don't expect it to be used as a security measure. I just avoid logging important information that would allow a user easily know how the system behaves. Of course anyone is able to see that using RedGate's Reflector or such kind of tools.It just prevents an average user to try to do something. But any advanced .net developer will be able to bypass this "security" measure.
Diego Jancic