![]() |
log4net Dynamic Config | |
| 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"; |
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