Friday, January 25, 2013

Configuring a log4net rolling file appender

In log4net, we have the options to configure a rolling file appender either via code or config. We would see both approaches on how we can configure it.

Configuring rolling file appender via config:

1. Create a sample Windows application and add reference of the log4net.dll
2. Now open the app.config and add the below "configSections" element under the "configuration" element.


<configSections>    
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

3. Now add the below "log4net" element just below the "configSections" element

<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MySampleLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="Amazon" />
<acceptOnMatch value="false" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>

NOTE: The above configuration does write the log to the file on 2 parameters which are Date and FileSize

4. Now in the startup form add a private property as shown below.


Private Shared ReadOnly log As ILog = LogManager.GetLogger(GetType(FileSplitter).Name)

5. Now in your startup form of your project, add a constructor and in the constructor paste the below code. This piece of code configures log4net.

Public Sub New()
   ' This call is required by the designer.
InitializeComponent()
 
   ' Add any initialization after the InitializeComponent() call.
log4net.Config.XmlConfigurator.Configure()
log.Info("Logging has been configured")
End Sub

Now start your application. Open the folder from where the application is executing and you can see a log file created and when you open the log file it has the message "Logging has been configured"

Configuring rolling file appender via code:

1. Create a sample Windows application and add reference of the log4net.dll
2. Now open the app.config and add the below "configSections" element under the "configuration" element.

<configSections>    
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

3. Now open the main Form of the project which is shown at startup (In our example it would be Form1.vb) and in the constructor paste the code below after "InitializeComponent()" method.

Dim fileappender = New log4net.Appender.RollingFileAppender()
fileappender.AppendToFile = True
fileappender.Threshold = log4net.Core.Level.Debug
fileappender.File = "MyLogFile_"
fileappender.DatePattern = "yyyyMMddhhmm"
fileappender.StaticLogFileName = False
fileappender.Layout = New log4net.Layout.SimpleLayout()
fileappender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date
fileappender.ActivateOptions()
DirectCast(log4net.LogManager.GetRepository(), log4net.Repository.Hierarchy.Hierarchy).Root.AddAppender(fileappender)
log4net.Config.BasicConfigurator.Configure(fileappender)
log.InfoFormat("Hurray. I have configured a rolling file appender via code.")


Now when you run your project you can see that there is a MyLogFile_<todays date>.txt file that is created in the output directory and the message "Hurray. I have configured a rolling file appender via code." is written to it.