Introduction
The Apache Software Foundation provides support for the open-source software project since 1999 (as far as I remember), which provide software product to the public. Having taking the advantage of that I am using log4net open source library in all my .Net application in so many years, to log output.
In this article I am going to discuss about how to maintain multiple log files in one application using log4net. You can select the different log files to log programmatically based on the relevant condition.
By clicking the following links you can read more about log4net.
http://projects.apache.org/projects/log4net.html
http://logging.apache.org/log4net/
The Basis
On the web you can find so many articles which are explain about the basis on log4net, which is about the configuration, the code setup and then call. Here I am not going to talk about that and I assumed that you know the basis on those. So let’s dig in!
Note: For the explanation purpose I am working on with a simple desktop application.
The Configuration
The recommended way to set up log4net logger is to configure the app.config (or the web.config in a web application) in a desktop application. The whole idea of using the config file is to do the configuration changes without re-compiling the application. The interesting configuration element is the appender:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\%property{LogFileName}" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5KB" />
<staticLogFileName value="true" />
<countDirection value="1" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd hh:mm:ss}] - [%-5p] – %m%n" />
</layout>
</appender>
As you know file attribute specified the log file that appender will write into it. You can specify all the other attributes as you wish in order to handle your application logging requirement.
PatternString Class
This class implements a patterned string, which accepts a pattern and renders it into a string. To change the file attribute I introduced pattern as follows,
<file type="log4net.Util.PatternString" value="Logs\%property{LogFileName}" />
Conversion pattern property is used to output a specific context property, in format %property{key}, which include the value from the property that keyed by the string ‘key’.
The Code Setup
Once you configured the config file appropriately all you have to do is setting the global variable before calling the XmlConfigurator. Here what I am usually does,
log4net.GlobalContext.Properties["LogFileName"] = strLogFileName;
log4net.Config.XmlConfigurator.Configure();
And the rest is usual.
Conclusion
Well, that is all about for this time. I believe that I have covered enough information about the managing multiple log files in a single application using log4net. If you have any concerns please let me know. By the time look at the C# project I have provide for a complete example.
History
- 06/08/2012 - Initial Version