Getting Started with Log4Net

0 comments

In most of my .net application development i will use Log4Net for my logging purposes. The best thing about this logging service because it was easy to configure, open source and you can have multiple types of logging as you wanted.

Here i will show you how to use this services inside your application. Here are the steps :

1. Download log4net from http://logging.apache.org/log4net/download.html


2. Once downloaded unzip the file to a suitable location. Here i unzip the package to C:\Log4Net


3. Open Visual Studio and create a new console application.


4. Add to the project a reference to the C:\log4net\bin\net\2.0\release assembly to the project.


5. Add a new xml file named app.config to your project.


6. Modify the xml accordingly to accommodate the log4net configuration as below :

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
</configSections>
<log4net>
<appender name="app" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\app.log"/>
<appendToFile value="true"/>
<datePattern value="yyyyMMdd"/>
<rollingStyle value="Date"/>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true"/>
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date - %message%newline"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="app"/>
</root>
</log4net>
</configuration>

From the above xml config, you can clearly see that we need to add 'log4net.Config.Log4NetConfigurationSectionHandler, log4net' inside the config section.

In the <log4net> tag you need to set a few setting for your logging. The first setting need to define is the logging type. for this example i have choose to log the message into a file appender (log4net.Appender.RollingFileAppender). The log file will be created in my C:\Logs\app.log.

Second, inside the <filter> tag i have set that this type of logging will accept a message with the log level from INFO to FATAL type, which mean a message represented as info and all the way to fatal will be log into the same file.

After that i have set the way how the log message will be written inside the log file. in this case please refer to '%date - %message%newline'. This will tell log4net to write the message in a format like '2011-06-14 14:40:31,844 - example'. The %date represent the datetime, %message represent the log message and %newline represent a line break or new line.

Lastly you need to tell log4net which of the logging you wish to use. Inside the <root> tag i have define that the logging will use the 'app' appender. We can define multiple appender and only use the one that we wish to use.

7. Next we need to write the main method as this :

Module Module1

Private ReadOnly logger As Log4Net.ILog = Log4Net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString)

Sub Main()
Log4Net.Config.XmlConfigurator.Configure()

logger.Debug("THis is my world!")
logger.Info("How beautiful the console looks like")
logger.Warn("You are great you did this")
logger.Error("Who make you know is the best")
logger.Fatal("The world the great")
End Sub

End Module

Take note that we need to include the line 'Private ReadOnly logger As Log4Net.ILog = Log4Net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString)' as a declaration whenever we need to use the log4net. The line 'Log4Net.Config.XmlConfigurator.Configure()' needs to be applied whenever the code started as this will tell log4net to configure the logging based on the define setting inside our app.config. The configure only needed to be run once inside our application ie on application start or initialize.

8. Compile and run the application. You can see that a file name app.log will be created in C:\Logs folder. The file will display :

2011-09-08 23:15:34,747 - How beautiful the console looks like
2011-09-08 23:15:34,815 - You are great you did this
2011-09-08 23:15:34,815 - Who make you know is the best
2011-09-08 23:15:34,816 - The world the great

Please note that the debug message is not in the file because we have configured log4net appender to log message from INFO to FATAL. The hierarchy of the message level from lower to upper is from debug to fatal as shown in the above code.

This conclude the usage and example of Log$Net. Pretty easy right? :D