Introduction
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
="1.0"="utf-8"
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<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] <%X{auth}> - %m%n" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<category name="testApp.LoggingExample"><priority value="ALL" /></category>
</log4net>
</configuration>
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
="1.0"
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler"/>
</configSections>
<appSettings>
</appSettings>
<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"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="LogFileAppender"/>
</root>
<category name="my.category"><priority value="DEBUG"/></category>
</log4net>
<system.web>
…………..
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.
log4net.Config.DOMConfigurator.Configure();
We can also load the configuration from different places or URLs.
log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo(@"<your config file>");
WebRequest myWebRequest = WebRequest.Create("<Config File URL>");
WebResponse myWebResponse = myWebRequest.GetResponse();
log4net.Config.DOMConfigurator.Configure(myWebResponse.GetResponseStream());
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");
History
- 16th August, 2005: Initial post