Introduction
Apache log4net is a framework to help developers output several types of logs as needed. It is designed to support .NET for web and desktop application development.
Background
This guide is written to help beginner developers to implement apache log4net in their web applications. It is made as simple as possible, yet still adequate to cover some important settings of apache log4net.
Implementation Guide
Let us get started by following some steps below:
1. Download Log4net Library
For implementing log4net in ASP.NET web applications, we will need log4net.dll
file.
This file can be downloaded from here, go to Download
section, then download the Binaries
file.
2. Add Reference to Web Application Project
In Solution Explorer window, right click on the project and choose Add Reference...
Choose Browse
tab and point to log4net.dll you just downloaded.
3. Add Assembly Reference to Web Application Project
In Solution Explorer window, click Show All Files
on top
Under the project choose My Project
folder and double click on AssemblyInfo.vb
Add our assembly reference for log4net:
<Assembly: XmlConfigurator(ConfigFile:="log4net.config",Watch:=True)>
Config File
This config file part is used to declare specific log4net config file, in the example, I put it under root folder of web application called log4net.config
. If it is not specified (removed), we will need to specify the log4net config inside web.config
in root directory of web application.
Watch
When it is set to True
, the log4net will auto reload the config once it is modified.
4. Add Config to Web Application Project
- In Solution Explorer window, add new file called
log4net.config
(in root directory of web application) - Double click the file and copy paste the code below:
="1.0"
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="GeneralAppender" />
</root>
<logger name="GeneralLogger">
<level value="ALL" />
<appender-ref ref="GeneralAppender" />
</logger>
<appender name="GeneralAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\Logs\BasicSample.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100K" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] - [%logger] %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Root
All loggers will follow the property listed here if they do not have their own logger configuration:
<level>
This tag is used to ensure which logging level is going to be logged according to hierarchy lowest to highest (possible values are OFF
, DEBUG
, INFO
, WARN
, ERROR
, FATAL
and ALL
). For example, if WARN
is chosen, then WARN
, ERROR
, FATAL
will be logged.
<appender-ref>
Reference to which Appender is used to write to file
Logger
Specifically declare configuration for a logger (same properties with root).
Appender
Appender part is used to config the file writer.
<file>
This part is used to specify file path in the web server to save the log file.
<appendToFile>
This part possible values are true and false. True means append to the last part of the file, while False means overwritten the file
<rollingStyle>
Possible values are Size
, Date
and Composite
.
- Size: log4net will create a new file once size is reached
- Date: log4net will create a new file once change date
- Composite: log4net will create a new file once size is reached/change date
<datePattern>
This part is used for file naming. It is only used for date/composite rolling style, while for size rolling style this part can be removed.
<maxSizeRollBackups>
This part is to specify how many files will be kept for size/composite rolling style, while for date rolling style this part can be removed.
<maximumFileSize>
This part is to specify maximum file size for size/composite rolling style, while for date rolling style, this part can be removed.
<layout>
This part is used to specify which type of appender is going to be used. The most commonly used is RollingFileAppender
, which writes log to file with rolling options (other appender types can be seen in here).
5. Import Library in Web Page
We will need to import log4net class to the page level (*.aspx.vb) to use log4net library:
Imports log4net
6. Start Logging!
Below is code sample how to use log4net for logging in the page level (*.aspx.vb).
Dim logger As ILog = Nothing
logger = LogManager.GetLogger("GeneralLogger")
logger.Debug("This is debug message")
logger.Info("This is info message")
logger.Warn("This is warn message")
logger.Error("This is error message")
logger.Fatal("This is fatal message")
Basically, the code above loads the log4net config for a logger as specified in web.config (in this case is GeneralLogger
). Then start writing the log.