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.
Log4net is very simple to use yet powerful logging option. This article describes log4net usage in a web application in six easy steps.
[assembly: log4net.Config.XmlConfigurator(ConfigFile="Web.config", Watch=true)] //For log4net 1.2.10.0Above statement provides the information about config file in which log4net configuration parameters are defined.
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<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" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Above section defines the configuration parameters to be used for logging.2006-07-14 20:26:04,033 [1736] ERROR Utility [PayStub] - Could not find a part of the path
"c:\inetpub\wwwroot\TestProj\Template\PayStub.xml"
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
</appSettings>Add below lines after system.web section: <system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\\TestProj\\TestProjlog4net.txt" />
</listeners>
</trace>
</system.diagnostics>
a. Add namespace
using log4net;
b. Add below declarations within class definition
private static readonly ILog log = LogManager.GetLogger(typeof(TestPage1).Name);OR
private static readonly ILog log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
if (log.IsErrorEnabled)
{
log.Error("Page Load failed : " + ex.Message);
}OR 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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||