Logging is the part of development which is normally not included in Product / Software release but at the same time it is necessary to create the logging mechanism in your software module.
Without logging, how you will track
- run time errors and exceptions in Websites
- Status of Windows service.
- Softwares which will run in background.
- Crash reports etc.
In this article i am going to explain that how to configure the log 4 net in you .net based applications.
To start with, you have to download the log 4 net from here.
After downloading, you will need to set the configurations related to Log4net. (web.config in case of websites else app.config)
code snapshot in configuration file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<file value="Logs/Notification" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<StaticLogFileName value="false"/>
<datePattern value="-dd-MMM-yy'.log'" />
<!--Roll every Day-->
<maxSizeRollBackups value="-1" />
<!--Infinite backups-->
<maximumFileSize value="2MB" />
<!--Maximum size of 1 file-->
<layout type="log4net.Layout.PatternLayout">
<!-- http://logging.apache.org/log4net /release/sdk/log4net.Layout.PatternLayout.html -->
<conversionPattern value="%date{dd-MMM-yy, HH:mm:ss} - %5level [%thread] - %message%newline " />
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="EMAIL" />
<from value="EMAIL" />
<subject value="Error in Component" />
<smtpHost value="SMTP SERVER" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="FATAL"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%5level [%thread] [%file]- %message%newline " />
</layout>
</appender>
<root>
<!-- Ascending Priority: Debug,Info,Warn,Error,Fatal,ALL -->
<priority value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="SmtpAppender" />
</root>
</log4net>
You can find the description inline in configuration.
For more info, you can visit this site.
To use log 4 net in code, after configuration you have to initialize the object of log 4 net like below line of code.
private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
After initialization, We have to configure the log 4 net.
XmlConfigurator.Configure();
To log we can decide the log levels:
There are five log levels:
- Fatal (Highest Level)
- Warn
- Error
- Debug
- Info (Lowest Level)
XmlConfigurator.Configure();
logger.Info(new Exception("This is test Exception"));
logger.Debug(new Exception("This is test Exception"));
logger.Error(new Exception("This is test Exception"));
logger.Warn(new Exception("This is test Exception"));
logger.Fatal(new Exception("This is test Exception"));

Leave a Reply to Mathew BirdsellCancel reply