Click here to Skip to main content
14,826,454 members
Articles / Web Development / ASP.NET
Posted 8 May 2007


46 bookmarked

Use Multiple log4net Outputs from One Application

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
8 May 2007CPOL1 min read
By using this article, you can easily configure multiple output log files from log4net


This is an article simply to demonstrate how to use several output log files depending on the library or the application.

Using the Code

Developers who develop class libraries may need to log details separate from the main application to support users or to quickly retrieve the data. But when they try to implement this, they will face a problem. This is because normally log4net has only one config section under the ASP.NET web.config file or Windows application app.config file. To overcome this problem, you can use different logger entries which define the output.

Default case:

    <appender type="log4net.Appender.RollingFileAppender" name="RollingFile">
        <file value="c:\\logs\\Log.txt" />
        <layout type="log4net.Layout.PatternLayout" />
            <conversionpattern value="%d [%t] %-5p %c - %m%n" />

        <level value="ALL" />
        <appender-ref ref="RollingFile" />

When you specify the <root /> tag, it says all the log data will log to that output file. To stop this, we need to remove the <root /> element and have separate <logger /> tags for every library or the application. You also need to specify your root namespace for the library.

    <appender type="log4net.Appender.RollingFileAppender" name="classApp1">
      <file value="c:\\Library1.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />

    <appender type="log4net.Appender.RollingFileAppender" name="classApp2">
      <file value="c:\\Library2.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />

    <appender type="log4net.Appender.RollingFileAppender" name="application">
      <file value="c:\\Application.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%d [%t] %-5p %c - %m%n" />

    <logger name="ClassLibrary1">
      <level value="ERROR" />
      <maximumfilesize value="256KB" />
      <param value="ERROR" name="Threshold" />

      <appender-ref ref="classApp1" />

    <logger name="ClassLibrary2">
      <level value="WARN" />
      <maximumfilesize value="256KB" />
      <param value="WARN" name="Threshold" />

      <appender-ref ref="classApp2" />

    <logger name="WindowsApplication1">
      <level value="WARN" />
      <maximumfilesize value="256KB" />
      <param value="WARN" name="Threshold" />

      <appender-ref ref="application" />


You can see three <logger /> items and separate <appender />tags referenced from those loggers. "name" of the <logger /> will specify the root namespace of the library or the application. Specify which appender you need to refer from the logger. When executing the application, if something is written to the log from the ClassLibrary2.Class2, it will refer the <logger name="ClassLibrary2" /> entry. This means the output will be on c:\\Library2.txt file. This way you can specify any amount of loggers dynamically.

Note: The code in Visual Studio 2005 format.

Points of Interest

With this option, we can customize our log details as required. Per class logger is also possible.


  • 9th May, 2007: First version


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


About the Author

Charith M
Web Developer
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

QuestionDoes this work with .Net 4.0? I can't get it to work with 4.0 Pin
anhlunnhaque7-Mar-12 9:21
Memberanhlunnhaque7-Mar-12 9:21 
AnswerRe: Does this work with .Net 4.0? I can't get it to work with 4.0 Pin
Charith M8-Mar-12 19:58
MemberCharith M8-Mar-12 19:58 
GeneralMy vote of 5 Pin
Rattler070121-Feb-11 16:50
MemberRattler070121-Feb-11 16:50 
QuestionHow to setup default logger? Pin
HeinzTomato18-Mar-10 0:58
MemberHeinzTomato18-Mar-10 0:58 
AnswerRe: How to setup default logger? Pin
Charith M18-Mar-10 1:47
MemberCharith M18-Mar-10 1:47 
GeneralMultiple files from same class Pin
Giorgi Dalakishvili9-Jan-09 10:52
mentorGiorgi Dalakishvili9-Jan-09 10:52 
GeneralRe: Multiple files from same class Pin
Charith M11-Jan-09 16:09
MemberCharith M11-Jan-09 16:09 
GeneralRe: Multiple files from same class Pin
Maheswaran_S9-Dec-15 22:40
MemberMaheswaran_S9-Dec-15 22:40 
SuggestionRe: Multiple files from same class Pin
Maheswaran_S9-Dec-15 22:41
MemberMaheswaran_S9-Dec-15 22:41 
GeneralSystem.Diagnostics.Trace Pin
Paul A. Howes17-May-07 3:09
MemberPaul A. Howes17-May-07 3:09 
GeneralRe: System.Diagnostics.Trace Pin
micdev4210-Oct-08 22:02
Membermicdev4210-Oct-08 22:02 
GeneralRe: System.Diagnostics.Trace Pin
Paul A. Howes11-Oct-08 1:48
MemberPaul A. Howes11-Oct-08 1:48 
QuestionSource code for log4net Pin
jaba.rich15-May-07 1:05
Memberjaba.rich15-May-07 1:05 
AnswerRe: Source code for log4net Pin
Charith M15-May-07 2:17
MemberCharith M15-May-07 2:17 

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.