Saturday, July 25, 2009

log4net gotcha: AppDomains

@YaronNaveh

We are using the log4net logging framework in a new project.
In one initialization point of the application we init it:


using log4net;
using log4net.Config;

XmlConfigurator.Configure(new FileInfo("log4net.config"));


And in many other locations we use it:


ILog log = LogManager.GetLogger(typeof(SomeClass));
log.Debug("some debugging message");


In one of my classes the messages I've logged did not appear in the log file (or in any of the other appenders). When I explicitly re-initialized log4net in the same class the messages were written successfully. A short investigation found the reason: This class is called from a different AppDomain than the other classes. log4net's LogManager class is static, which limits its scope to the calling AppDomain.

Conclusion: log4net needs to be initialized once per AppDomain.

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

0 comments: