Click here to Skip to main content
15,312,535 members
Articles / Web Development / IIS
Posted 15 Aug 2005


37 bookmarked

Configure Log4Net for Desktop and Web Applications

Rate me:
Please Sign up or sign in to vote.
3.32/5 (20 votes)
15 Aug 2005CPOL1 min read
How to configure Log4Net for desktop and Web applications


This article describes how to configure Log4Net to work with your desktop and Web applications. Even though this Web site contains everything you ever need to work with this Log4Net library, if you are new to .NET and this logging framework, and would like to quickly configure an environment so that you can start experiencing the framework, this article is worth going through.

Step 1: Compose the Configuration Information for Log4Net

Log4Net Framework uses the custom config handler to load in the configuration information. So to configure it, we can put the configuration in the App.Config for Desktop application and Web.Config for Web application. The following sections are examples of the same.

Desktop Application

Add the Log4Net configuration to the App.Config file. For more information about the configuration, refer to this page.

Sample App.Config File

<?xml version="1.0" encoding="utf-8" ?>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, 
    log4net" />
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="c:\\hnguyen\\download\\error-log.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n" />
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
<category name="testApp.LoggingExample"><priority value="ALL" /></category>

Web Application

Add the Log4Net configuration information to your web.config file. For more information about the configuration, refer to this page.

Sample web.config

<?xml version="1.0"?>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler"/>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net">
<param name="File" value="C:\\temp\\test-web-log.txt"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
<priority value="ALL"/>
<appender-ref ref="LogFileAppender"/>
<category name="my.category"><priority value="DEBUG"/></category>

Step 2: Load Configuration

Before we can log any message, we need to tell the Log4Net Framework to load its configuration. Do this by invoking the DOMConfigurator once in your application.

//Load from App.Config file

We can also load the configuration from different places or URLs.

//Load from local file
log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo(@"<your config file>");

//Load from URL
WebRequest myWebRequest = WebRequest.Create("<Config File URL>");
WebResponse myWebResponse = myWebRequest.GetResponse();

Step 3: Create a Log Category

After loading the configuration, we need to create a log category before logging any message. Do this by using the LogManager class.

private readonly ILog logger = LogManager.GetLogger("testApp.LoggingExample");


  • 16th August, 2005: Initial post


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


About the Author

Hai, Nguyen
Web Developer
United States United States
Senior Software Engineer

Comments and Discussions

GeneralMy vote of 1 Pin
Arijit_Mukherjee19-Feb-14 22:31
MemberArijit_Mukherjee19-Feb-14 22:31 
GeneralMy vote of 2 Pin
pdelco9-Dec-09 8:50
Memberpdelco9-Dec-09 8:50 
GeneralAnother good link for setting and implement log4net Pin
MD.Tanvir Anowar15-Nov-09 16:17
MemberMD.Tanvir Anowar15-Nov-09 16:17 
GeneralThank you for the post. Pin
Cheon sang3-Aug-09 3:36
MemberCheon sang3-Aug-09 3:36 
GeneralLog4net for Web Application Pin
Manjunath Kotegowda25-Jun-09 19:42
MemberManjunath Kotegowda25-Jun-09 19:42 
QuestionWhat about silverlight/xaml? Pin
devvvy12-Feb-09 1:30
Memberdevvvy12-Feb-09 1:30 
GeneralThanks Pin
fun-e23-May-08 0:18
Memberfun-e23-May-08 0:18 
GeneralThanks Hai! Pin
eggshelly11-Jul-07 21:50
Membereggshelly11-Jul-07 21:50 
General25 Information messages.... Pin
haiaw27-May-06 13:23
Memberhaiaw27-May-06 13:23 
QuestionRelease 2.0? Pin
wirthmaster8-Feb-06 2:29
Memberwirthmaster8-Feb-06 2:29 
AnswerRe: Release 2.0? Pin
P.J. van de Sande3-Jun-06 7:46
MemberP.J. van de Sande3-Jun-06 7:46 
GeneralHmmm Pin
Giles15-Aug-05 23:42
MemberGiles15-Aug-05 23:42 
GeneralSome more effort wouldbe great Pin
Mark Focas15-Aug-05 20:43
MemberMark Focas15-Aug-05 20:43 

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.