Click here to Skip to main content
15,794,593 members
Articles / Web Development / ASP.NET

Log4Net with ASP.NET 3.5 – File Appender

Rate me:
Please Sign up or sign in to vote.
4.61/5 (19 votes)
9 Jun 2009CPOL2 min read 132.4K   5.3K   24   9
Log4Net with ASP.NET 3.5


At production time, you can't debug your application and sometimes errors that appear in the production server don't appear in the development or in preProd server. So if you try to get the error to solve it in production, you need to find a way to help you in getting the error and tracing your application at production time without stopping or using debug mode in application.

Log4net is a tool to help the programmer output log statements to a variety of output targets in production, PreProd or development server.

Here we will start explaining how to install and configure this tool to work with ASP.NET 2.0, 3.0, 3.5 using C#.

Using log4net will help you to collect errors in many ways:

  1. FileAppender (collect messages in file)
  2. SMTPAppender (collect messages in email)
  3. ADONetApp<code>ender (collect messages in database)
  4. EventLogAppender (collect messages in event viewer)

There are many other ways, but this is a common and useful way.

In this article, I'll explain how to use file appender.

Using File appender helps you to print all messages you need in file. The file will be appended to rather than overwritten each time the logging process starts.

Using the Code

Let's start:

  1. Create your website application. Let's name it MyloggerSite
  2. Download the DLL file from Apache web site (
    After downloading extract the file.
  3. After downloading and  creating your application, you need to add Log4net DLL reference to your web site, right click in your application and choose add reference...


    Then click browse tab.

    Point to the folder you just extracted and choose the DLL file in this path:


  4. Now let's configure our application by adding some tags to web.config file:

    Inside <configSections> tag, add this tag:

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, 
  5. After </system.web> closing tag, we will add a new tag for Log4net.
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
          <param name="File" value="c:\\ MyloggerSite.log"/>
          <!-- Example using environment variables in params -->
          <!-- <param name="File" value="${TMP}\\ApplicationKit.log" /> -->
          <param name="AppendToFile" value="true"/>
          <layout type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%d [%t] %-2p %c [%x] - %m%n"/>
                <level value="ALL"/>
                <appender-ref ref="FileAppender"/>
    <level value="ALL"/>

    You can change all values to any of this list:

    1. ALL
    2. DEBUG
    3. INFO
    4. WARN
    5. ERROR
    6. FATAL
    7. OFF

    They are listed above from lowest to highest priority.

  6. Now we need to add Global Application class to our application (Global.asax).

    Open your Global.asax file and add the following code in the Application_Start() method:

    void Application_Start(object sender, EventArgs e)
  7. Now your log4net is ready to use.

Open default.aspx.cs file and import log4net library.

using log4net;
using log4net.Config;
then lets  declare the logger var.
ILog logger = log4net.LogManager.GetLogger(typeof(_Default));
//ILog logger = log4net.LogManager.GetLogger("type any thing you want");
But better to use the class file as I did first [typeof(_Default)].
ILog logger = log4net.LogManager.GetLogger(typeof(_Default));
    protected void Page_Load(object sender, EventArgs e)
        logger.Info("Hello Nine Thanks for use Log4Net,This is info message");
        logger.Debug("Hello Nine Thanks for use Log4Net,This is Debug message");
        logger.Error("Hello Nine Thanks for use Log4Net,This is Error message");
        logger.Warn("Hello Nine Thanks for use Log4Net,This is Warn message");
        logger.Fatal("Hello Nine Thanks for use Log4Net,This is Fatal message");      

Open your C: drive and you'll see a file with the name MyloggerSite.log is created and our message has been added to it.


Our message:


Try to change:

<level value="ALL"/>


<level value="Error"/>

The last thing we need to explain here is the message format we printed:

<param name="ConversionPattern" value="%d [%t] %-2p %c [%x] - %m%n"/> 

<param name="ConversionPattern" 
    value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />

Compare these two lines respectively.

For more information, click here.

Good luck!


  • 9th June, 2009: Initial post 


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

Written By
Software Developer www.ITPATH.NET
Yemen Yemen
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionProblem with log4net with 3.5 Pin
Member 1102118924-Apr-15 9:43
Member 1102118924-Apr-15 9:43 
QuestionConfiguration changes Pin
saboor awan30-Oct-13 21:19
saboor awan30-Oct-13 21:19 
QuestionGreat article!!! Pin
Member 878911425-Dec-12 4:05
Member 878911425-Dec-12 4:05 
GeneralA mi me gusto! Pin
foluis29-Oct-12 7:25
foluis29-Oct-12 7:25 
QuestionMyloggerSite Pin
WebMaster9-Oct-12 20:38
WebMaster9-Oct-12 20:38 
GeneralDOMConfigurator is obsolete Pin
User-Rock4-Jan-11 22:47
User-Rock4-Jan-11 22:47 
RantMy vote of 1 Pin
SteveCasey11-Jun-09 1:39
SteveCasey11-Jun-09 1:39 
GeneralRe: My vote of 1 Pin
NiN9E12-Jun-09 22:07
NiN9E12-Jun-09 22:07 
General[My vote of 2] Has nothing to do with 3.5 Pin
Ramon Smits11-Jun-09 0:20
Ramon Smits11-Jun-09 0:20 

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.