I'm having a problem where ConfigureAndWatch won't craps out when trying
to reload the configuration file after it's been modified. In my
global.asax.cs file, I have the following:
protected void Application_Start(Object sender, EventArgs e)
{
DOMConfigurator.DOMConfigurator.ConfigureAndWatch(new
System.IO.FileInfo(Server.MapPath("log4net.config")));
Logger.Info("Application Started");
}
The logger works fine until I change log4net.config, at which point it
writes out the footer content and then stops logging. When looking at
the log4Net internal logging, I see that the error being thrown is that
the HttpContext is not available during the DOMConfigurator's attempt to
reload itself from the log4net.config file. The HttpContext is used by a
helper class that determines the location of the log output file:
public class CustomWebLogFileAppender : log4net.Appender.FileAppender
{
public override string File
{
get{return base.File;}
set{base.File = Web.WebEnvironment.LogDirectory + value;}
}
}
It looks like this happens because the configuration reload takes place
when the file is touched, and not with subsequent web requests. Does
anyone have a suggestion as to how I might be able to determine if
log4net is configured so that I can reload it programmatically, if need
be? I'm thinking I'll do something like this:
public class Logger
{
public static void Debug(string aMessage)
{
ILog aLog = GetCurrentLogger();
if (aLog.IsDebugEnabled) aLog.Debug(aMessage);
}
static private ILog GetCurrentLogger()
{
if (<<The configuration isn't loaded>>)
DOMConfigurator.ConfigureAndWatch(<<the config file>>);
return LogManager.GetLogger(<<the type>>);
}
}
-Josh