Ok, I found a solution. Should have stuck with it for a little while
longer... Turns out that I needed to see if any appenders are still
configured. In case anyone is looking for this in the future:
static private ILog GetCurrentLogger()
{
ILog aLog = LogManager.GetLogger(new
StackTrace().GetFrame(2).GetMethod().DeclaringType.FullName);
if
(((log4net.Repository.Hierarchy.Hierarchy)aLog.Logger.Repository).Root.A
ppenders.Count==0)
{
log4net.Config.DOMConfigurator.ConfigureAndWatch(new
FileInfo(HttpContext.Current.Server.MapPath("log4net.config")));
if (aLog.IsDebugEnabled) aLog.Info("Detected a Log4Net
configuration file change");
}
return aLog;
}
I'd bet my last buck that this isn't thread safe, and may not work in an
environment where log4net is using in a more advanced manner, but it
serves my requirements. Feedback and further insight on my
implementation is welcome.
-Josh
-----Original Message-----
From: Josh Kewley [mailto:[EMAIL PROTECTED]
Sent: Tuesday, November 30, 2004 5:56 PM
To: Log4NET User
Subject: ConfigureAndWatch and HttpContext
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