Click here to Skip to main content
15,312,535 members
Articles / Web Development / ASP.NET
Posted 14 Jul 2006


134 bookmarked

How to use log4net

Rate me:
Please Sign up or sign in to vote.
4.31/5 (66 votes)
14 Jul 2006CPOL2 min read
This article describes the basic steps to be followed for using Log4net in a web application.
Download source code - 103.9 Kb


This article describes the basic steps to be followed for using Log4net in a web application. Log4net is an open source library that allows .NET applications to log statements to a variety of targets.


This article assume you have basic working knowledge of C#, and ASP.NET programming.

Using the code

Log4net is very simple to use yet powerful logging option. This article describes log4net usage in a web application in six easy steps.

  • Step 1: Get the latest version of log4net library and add reference of it in your project.

  • Step 2: Add below line in your AssemblyInfo.cs file.
    [assembly: log4net.Config.XmlConfigurator(ConfigFile="Web.config", Watch=true)]   //For log4net
    Above statement provides the information about config file in which log4net configuration parameters are defined.

  • Step 3: Add below section in Web.Config file.
    	<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <log4net debug="true">
    	<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    		<file value="C:\\TestProj\\TestLog.txt" />
    		<appendToFile value="true" />
    		<rollingStyle value="Size" />
    		<maxSizeRollBackups value="10" />
    		<maximumFileSize value="10MB" />
    		<staticLogFileName value="true" />
    		<layout type="log4net.Layout.PatternLayout">
    			<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    		<level value="DEBUG" />
    		<appender-ref ref="RollingLogFileAppender" />

    Above section defines the configuration parameters to be used for logging.

    RollingLogFileAppender describes the appender to be used for logging. This means that the log should be written in a file, which will rollover when full. There are several other appender available and more than one appender can be attached to a logger.

    The layout is responsible for formatting the logging request, whereas an appender takes care of sending the formatted output to its destination. Layout used above would format the output as below:
    2006-07-14 20:26:04,033 [1736] ERROR Utility [PayStub] - Could not find a part of the path 

  • Step 4: If you want log4net to add its own diagnostics messages then add below lines in web.config file.
        <add key="log4net.Internal.Debug" value="true" />
    Add below lines after system.web section:
        <trace autoflush="true">
                <add name="textWriterTraceListener" 
                   initializeData="C:\\TestProj\\TestProjlog4net.txt" />

  • Step 5: Now follow below steps in your code-behind.

    a. Add namespace

    using log4net;

    b. Add below declarations within class definition

    private static readonly ILog log = LogManager.GetLogger(typeof(TestPage1).Name);
    private static readonly ILog log = LogManager.GetLogger(
  • Step 6: Now you are ready for logging. Use below statements to log your messages in log file:
    if (log.IsErrorEnabled)
        log.Error("Page Load failed : " + ex.Message);
    if (log.IsDebugEnabled)
        log.Debug("Application loaded successfully.");
    We have just scratched the surface of log4net. There are several other good features available in log4net and it can be used to collect vital information for several purposes. Happy Programming.


    • 07/14/2006 - Article submitted.
    • 07/19/2006 - Added Sample Project & updated the article.
  • License

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


    About the Author

    Suman Kumar
    India India
    No Biography provided

    Comments and Discussions

    QuestionProblem with Log4Net Pin
    Member 1102118921-Apr-15 9:18
    MemberMember 1102118921-Apr-15 9:18 
    AnswerRe: Problem with Log4Net Pin
    Member 1163319322-Apr-15 4:00
    MemberMember 1163319322-Apr-15 4:00 
    GeneralRe: Problem with Log4Net Pin
    Member 1102118922-Apr-15 23:59
    MemberMember 1102118922-Apr-15 23:59 
    GeneralMy vote of 3 Pin
    mahi379-Oct-14 3:20
    Membermahi379-Oct-14 3:20 
    GeneralMy vote of 5 Pin
    harsha.bhuyan21-May-13 1:49
    Memberharsha.bhuyan21-May-13 1:49 
    QuestionWhere is AssemblyInfo.cs Pin
    ariftk8-Jan-13 18:43
    Memberariftk8-Jan-13 18:43 
    AnswerRe: Where is AssemblyInfo.cs Pin
    Avil Mascarenhas16-Oct-14 0:26
    MemberAvil Mascarenhas16-Oct-14 0:26 
    GeneralMy vote of 5 Pin
    Kiran Kumar B10-Oct-12 20:20
    MemberKiran Kumar B10-Oct-12 20:20 
    QuestionRead the configuration Pin
    Haitham.Hussien17-Sep-12 2:15
    MemberHaitham.Hussien17-Sep-12 2:15 
    QuestionLog4Net Pin
    sivakarthiki9-Sep-12 6:14
    Membersivakarthiki9-Sep-12 6:14 
    GeneralMy vote of 4 Pin
    Member 859976627-Aug-12 22:55
    MemberMember 859976627-Aug-12 22:55 
    GeneralMy vote of 3 Pin
    yusia112-Jan-12 1:59
    Memberyusia112-Jan-12 1:59 
    Questiongood article Pin
    ajaytiwari12345630-Sep-11 1:16
    Memberajaytiwari12345630-Sep-11 1:16 
    GeneralMy vote of 2 Pin
    Jalal Khordadi24-Sep-11 0:36
    MemberJalal Khordadi24-Sep-11 0:36 
    GeneralMy vote of 4 Pin
    Herminio Tribaldos27-Jul-11 21:13
    MemberHerminio Tribaldos27-Jul-11 21:13 
    GeneralMy vote of 4 Pin
    RiteshPapneja20-Apr-11 22:03
    MemberRiteshPapneja20-Apr-11 22:03 
    GeneralNot working while executing the exe(not in debug mode) Pin
    NarVish27-Jan-11 19:08
    MemberNarVish27-Jan-11 19:08 
    GeneralMy vote of 3 Pin
    Pravin Patil, Mumbai17-Jan-11 20:53
    MemberPravin Patil, Mumbai17-Jan-11 20:53 
    GeneralMy vote of 1 Pin
    zeltera14-Oct-10 4:59
    Memberzeltera14-Oct-10 4:59 
    GeneralMy vote of 5 Pin
    pritam hinger15-Sep-10 23:40
    Memberpritam hinger15-Sep-10 23:40 
    GeneralMy vote of 5 Pin
    zippy198114-Sep-10 4:11
    Memberzippy198114-Sep-10 4:11 
    GeneralLog4net in Windows service Pin
    Jitendra Zaa7-Sep-10 21:08
    MemberJitendra Zaa7-Sep-10 21:08 
    GeneralNot working Pin
    ravileeladhar21-Jul-10 23:40
    Memberravileeladhar21-Jul-10 23:40 
    GeneralRe: Not working Pin
    Suman Kumar23-Jul-10 3:00
    MemberSuman Kumar23-Jul-10 3:00 
    GeneralFurther tutorials Pin
    Member 41098946-Apr-10 6:30
    MemberMember 41098946-Apr-10 6:30 

    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.