Click here to Skip to main content
12,756,259 members (34,972 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 7 Jun 2012

Multiple log files from one application, using log4net.

, 8 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Learn how to manage multiple log files in a single application using log4net.


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. 

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" />

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;

And the rest is usual.


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.


  • 06/08/2012 - Initial Version


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


About the Author

Software Developer (Senior)
Sri Lanka Sri Lanka
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.

You may also be interested in...


Comments and Discussions

QuestionLog is written to single file Pin
Member 316876019-Mar-14 8:24
memberMember 316876019-Mar-14 8:24 
AnswerRe: Log is written to single file Pin
CodingLover21-Mar-14 18:32
memberCodingLover21-Mar-14 18:32 
QuestionMultiple Threads Pin
narva891724-Oct-12 10:32
membernarva891724-Oct-12 10:32 
AnswerRe: Multiple Threads Pin
CodingLover24-Oct-12 19:58
memberCodingLover24-Oct-12 19:58 
Question[My vote of 1] Was this article necessary? Pin
FatCatProgrammer8-Jun-12 7:11
memberFatCatProgrammer8-Jun-12 7:11 
AnswerRe: [My vote of 1] Was this article necessary? Pin
CodingLover8-Jun-12 16:50
memberCodingLover8-Jun-12 16:50 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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