Click here to Skip to main content
15,397,324 members
Articles / Programming Languages / C#
Posted 12 Sep 2004


85 bookmarked

An Introduction to the log4net logging library, using C# - Part 2

Rate me:
Please Sign up or sign in to vote.
4.52/5 (38 votes)
12 Sep 20043 min read
An article describing more basic use of the log4net library.


In my previous article, I discussed the basics of the log4net logging package. I gave an overview of the 5 levels of logging that the library provides, and examples of the different type of appenders that can be used for outputting data to your logs. In this article, I'd like to give some information on how to use log4net effectively.

Specifying different logging levels for different modules

Sometimes within your application, you will want more logging information from some areas, and less from others. Log4net facilitates this by allowing different logging levels for different loggers. An example of this is shown in LogTest3 and LogTest4.

using log4net;
using log4net.Config;

public class LogTest3
    private static readonly ILog logger = 
    static LogTest3()

    static void Main(string[] args)
        LogTest3 log3 = new LogTest3();
        LogTest4 log4 = new LogTest4();
    public void DoLogging()
        logger.Debug("Here is a debug log from LogTest3.");
        logger.Info("... and an Info log from LogTest3.");

public class LogTest4
    private static readonly ILog logger = 
    static LogTest4()

    public void DoLogging()
        logger.Debug("Here is a debug log from LogTest4.");
        logger.Info("... and an Info log from LogTest4.");


The configuration file for LogTest3 has the following lines of interest:

    <level value="FATAL" />
    <appender-ref ref="ConsoleAppender" />

<logger name="LogTest3">
    <level value="DEBUG" />

<logger name="LogTest4">
    <level value="INFO" />

Within this XML fragment, we can see that the root logging level is set to "FATAL". That means that only FATAL error messages are logged. We can see however that this is overridden for LogTest3 and LogTest4 to the "DEBUG" and "INFO" levels accordingly. When we run this application, we will see then that LogTest3 logs everything from DEBUG level whereas LogTest4 logs everything from INFO level onwards.

2004-09-12 21:45:51,546 [1060] DEBUG LogTest3 - Here is a debug log from LogTest3.
2004-09-12 21:45:51,546 [1060] INFO  LogTest3 - ... and an Info log from LogTest3.
2004-09-12 21:45:51,546 [1060] INFO  LogTest4 - ... and an Info log from LogTest4.

Using this technique of specifying different log levels for different parts of your application gives you a great flexibility.

Should I log or not?

In general, the more logging you have, the greater flexibility you have and the easier it is to find out what's going wrong with your program. Don't get me wrong, logging is no substitute for debugging your program thoroughly, but there's always the case when you have deployed your application and you want to know exactly what's going on.

Sometimes, the cost of logging can be expensive, particularly when performance of your application is paramount (imagine logging every DEBUG statement to a database). Obviously, the first thing to do here, is turn down the logging level so that only the relevant information is produced (e.g., WARNINGs and above). This is a good start, but what if the cost of construction of the logging message is great (e.g., you have an expensive .ToString() method)?

logger.Debug("Expensive object value="+ExpensiveObject.ToString());

The ILog interface (of which all your loggers are implemented) provides a set of methods that enable you to check if a particular logging level is being used. These methods are shown below:

bool IsDebugEnabled();
bool IsInfoEnabled();
bool IsWarnEnabled();
bool IsErrorEnabled();
bool IsFatalEnabled();

Using these functions, we can now change our logging code for the expensive object to:

if (logger.IsDebugEnabled())
    logger.Debug("Expensive object value="+ExpensiveObject.ToString());

Completely removing logging

If you are completely happy with your application and you want to turn off logging completely, you can do this by setting the logging threshold to "OFF" for any/all your appenders, as shown below:

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <threshold value="OFF" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />

The threshold can also be set to one of DEBUG, WARN, INFO etc. to set the threshold for the specified appender irrespective of the logger being used.

That's all

That's the end of the brief introduction to logging using log4net. I hope you've enjoyed these articles, and I hope I've convinced you that logging is worthwhile and fun!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Salter
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionLogger Section is not creating any affect Pin
Dishant Verma25-Oct-15 0:16
MemberDishant Verma25-Oct-15 0:16 
Questioncan i add the <threshold value="OFF" /> without building Pin
tikva3331-Jul-11 23:15
Membertikva3331-Jul-11 23:15 
Generalgood example Pin
Donsw24-Oct-10 10:36
MemberDonsw24-Oct-10 10:36 
Good example but just elaborates son your frist , go deeper or more
My Recent Article : CDC - Change Data Capture

GeneralMy vote of 4 Pin
Dhrubajit13-Jul-10 2:30
MemberDhrubajit13-Jul-10 2:30 
GeneralRe: My vote of 4 Pin
Trollslayer5-Nov-10 10:07
mentorTrollslayer5-Nov-10 10:07 
GeneralMy vote of 1 Pin
Dhrubajit13-Jul-10 2:30
MemberDhrubajit13-Jul-10 2:30 
GeneralMy vote of 1 Pin
Dhrubajit13-Jul-10 2:29
MemberDhrubajit13-Jul-10 2:29 
GeneralRe: My vote of 1 Pin
omonney28-Oct-10 2:05
Memberomonney28-Oct-10 2:05 
Questionwhere is the log output? Pin
avi91116-Jun-10 22:37
Memberavi91116-Jun-10 22:37 
QuestionWhy private static readonly ?? Pin
AmitB198216-Mar-10 20:31
MemberAmitB198216-Mar-10 20:31 
GeneralThank you for the tutorials Pin
Dirk Voss19-Mar-09 6:57
MemberDirk Voss19-Mar-09 6:57 
Generaldifferent loggers for different files Pin
Sheikh.Usman.Shakeel4-Apr-07 22:26
MemberSheikh.Usman.Shakeel4-Apr-07 22:26 
GeneralRe: different loggers for different files Pin
JoeSharp4-Apr-07 22:45
MemberJoeSharp4-Apr-07 22:45 
GeneralRe: different loggers for different files Pin
Sheikh.Usman.Shakeel8-Apr-07 20:08
MemberSheikh.Usman.Shakeel8-Apr-07 20:08 
GeneralXML Appender Pin
danielmgv12-Jul-05 1:57
sussdanielmgv12-Jul-05 1:57 
GeneralLog4Net Configuration setting Pin
Jeyaprakash,R.4-Jul-05 0:20
MemberJeyaprakash,R.4-Jul-05 0:20 
GeneralDavey let's update this to have ADONetAppeder as well Pin
Nirosh25-Apr-05 19:02
professionalNirosh25-Apr-05 19:02 
GeneralADONetAppender Pin
Nirosh30-Mar-05 0:15
professionalNirosh30-Mar-05 0:15 
GeneralNeed help Pin
enjoycrack18-Jan-05 0:20
Memberenjoycrack18-Jan-05 0:20 
GeneralRe: Need help Pin
David Salter18-Jan-05 8:38
MemberDavid Salter18-Jan-05 8:38 
GeneralRe: Need help Pin
enjoycrack18-Jan-05 14:52
Memberenjoycrack18-Jan-05 14:52 
GeneralRe: Need help Pin
David Salter19-Jan-05 8:07
MemberDavid Salter19-Jan-05 8:07 
GeneralRe: Need help Pin
enjoycrack19-Jan-05 14:43
Memberenjoycrack19-Jan-05 14:43 
GeneralUsing attribute on assembly to specify configurator Pin
Doug de la Torre15-Sep-04 11:21
MemberDoug de la Torre15-Sep-04 11:21 
GeneralRe: Using attribute on assembly to specify configurator Pin
David Salter16-Sep-04 10:07
MemberDavid Salter16-Sep-04 10:07 

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.