Nancy Street log4net Dynamic Config
Click to see the Site Map
HomeInfoMusicGalleryPetsGeoHobbiesGeo
Site MapWhat's NewRecent ChangesContactsServer StatisticsSite Information Home « Computers « DevBlog

Back to: Development Blog Contents

log4net Dynamic Configuration

In early 2005 I searched the web for a sample of how to dynamically configure log4net at runtime, without the need for a XML config file. There were plenty of comments hinting that it was possible, but there were no samples. Stepping through the config processing in the debugger for clues led to confusion.

Finally I stumbled across a confusing sample in a German website, which I eventually trimmed down to the following sample code that creates a logger with a trace appender and a rolling file appender

const string LogPattern = "%d [%t] %-5p %x %m%n";
Hierarchy hierarchy = (Hierarchy)LogManager.GetLoggerRepository();

TraceAppender tracer = new TraceAppender();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = LogPattern;
patternLayout.ActivateOptions();

tracer.Layout = patternLayout;
tracer.ActivateOptions();
hierarchy.Root.AddAppender(tracer);

RollingFileAppender roller = new RollingFileAppender();
roller.Layout = patternLayout;
roller.AppendToFile = true;
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.MaxSizeRollBackups = 4;
roller.MaximumFileSize = "100KB";
roller.StaticLogFileName = true;
roller.File = "foobar.txt"; <-- Actually a variable
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);

hierarchy.Root.Level = log4net.spi.Level.ALL;
hierarchy.Configured = true;

logger = LogManager.GetLogger("weblog");

I hope this will save other people the trouble of searching for a sample.

November 2008 Update: I realised recently that the only feature of log4net that I was really using was the "rolling file" facility. All of the other features with filtering and formatting with different severity levels and adapter destinations was a complete waste of code. One Saturday morning I spent about an hour writing my own "rolling file" class and used it to replace log4net in every application I owned.

It seems to be an irritating convention with logging libraries (log4net and EntLib for certain) that dynamically configuring the logging output is dreadfully complicated. Everything seems to be geared towards using the app cofiguration file, in which everything is hard-coded and you can't specify an output destination such as the user settings folder. To follow conventions, I like to send logging files to a folder like this:

C:\Documents and Settings\myname.MYDOMAIN\Local Settings\Application Data\My Company\My product\1.0

There is no way to specify a folder like this in the config file, so you have to go through the tedious processing of manually creating all of the logging classes and feed them the full path at runtime. My simple rolling file class encapsulates all of this tedium in a few lines of code.

Back to: Development Blog Contents


Contact Information | PGP Keys | Site Map | What's New | Visitor Book
Last Updated: 24-Nov-2008 22:06
Copyright © 1999-2007 Orthogonal Programming