Click here to Skip to main content
Click here to Skip to main content

Multiple log files from one application, using log4net.

By , 8 Jun 2012
 

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

CodingLover
Software Developer (Senior)
Sri Lanka Sri Lanka
Member
I graduated with a BSc honours degree in Computer Science & Engineering from Peradeniya University. Currently work as a Senior Software Engineer in Colombo.
 
I started my career with programming in Java, which I happen to master at later stages of my career. Then later stages I move with Microsoft technologies like C/C++, .Net, SQL Servers and Web 2.0 technologies. Currently re-learning PHP.
 
Now I'm spending my time with reading for my masters in Artificial Intelligence.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMultiple Threadsmembernarva891724 Oct '12 - 9:32 
AnswerRe: Multiple ThreadsmemberCodingLover24 Oct '12 - 18:58 
Question[My vote of 1] Was this article necessary?memberFatCatProgrammer8 Jun '12 - 6:11 
AnswerRe: [My vote of 1] Was this article necessary?memberCodingLover8 Jun '12 - 15:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 8 Jun 2012
Article Copyright 2012 by CodingLover
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid