Click here to Skip to main content
15,881,173 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 133.5K   5.3K   24   9
Log4Net with ASP.NET 3.5

Introduction

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 (http://logging.apache.org/log4net/download.html)
    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...

    AddReference.jpg

    Then click browse tab.

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

    DllLocation.jpg

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

    Inside <configSections> tag, add this tag:

    XML
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, 
    	log4net"/> 
  5. After </system.web> closing tag, we will add a new tag for Log4net.
    XML
    <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"/>
          </layout>
      </appender>
    
    <root>
                <level value="ALL"/>
                <appender-ref ref="FileAppender"/>
          </root>
    </log4net>
    
    In<root>
    
    <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:

    C#
    void Application_Start(object sender, EventArgs e)
        {
           log4net.Config.DOMConfigurator.Configure();
        }
  7. Now your log4net is ready to use.

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

C#
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.

LogFileLocation.jpg

Our message:

LogFileContent.jpg

Try to change:

XML
<root>
<level value="ALL"/>

to:

XML
<root>
<level value="Error"/>

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

XML
<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!

History

  • 9th June, 2009: Initial post 

License

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 asp.net 3.5 Pin
Member 1102118924-Apr-15 8:43
Member 1102118924-Apr-15 8:43 
QuestionConfiguration changes Pin
saboor awan30-Oct-13 20:19
saboor awan30-Oct-13 20:19 
QuestionGreat article!!! Pin
Member 878911425-Dec-12 3:05
Member 878911425-Dec-12 3:05 
GeneralA mi me gusto! Pin
foluis29-Oct-12 6:25
foluis29-Oct-12 6:25 
QuestionMyloggerSite Pin
WebMaster9-Oct-12 19:38
WebMaster9-Oct-12 19:38 
GeneralDOMConfigurator is obsolete Pin
User-Rock4-Jan-11 21:47
User-Rock4-Jan-11 21:47 
RantMy vote of 1 Pin
SteveCasey11-Jun-09 0:39
SteveCasey11-Jun-09 0:39 
GeneralRe: My vote of 1 Pin
NiN9E12-Jun-09 21:07
NiN9E12-Jun-09 21:07 
General[My vote of 2] Has nothing to do with asp.net 3.5 Pin
Ramon Smits10-Jun-09 23:20
Ramon Smits10-Jun-09 23: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.