Log 4 Net Overview
Log 4 Net Overview
Log 4 Net Overview
Introduction
Log4Net is an open source a .NET based set of assemblies, ported from the Apache Log4j
java classes, to provide a configurable logging framework. The classes allow for different
levels of logging based upon a hierarchical set of methods. Each module that utilise Log4Net
can allow separate areas of code to be configured for different levels of logging. In addition
there is a flexibility to allow class and namespace level logging if required. Log4Net is
configurable so that the format, type of log, log levels etc are not hard coded and can be
changed in a configuration file at run time (i.e. no program restart is required).
Installation
Copy the assembly files to a location on your hard drive and in Visual Studio, add a reference
to the log4net.dll assembly.
Code Changes
In order to utilise these classes the following needs adding to each class that requires
logging:
using log4net;
using log4net.Config;
namespace LoggerTest
{
public class Form1 : System.Windows.Forms.Form
{
private static readonly ILog log =
LogManager.GetLogger (typeof(Form1));
.
.
.
.
}
}
The LogManager.GetLogger line tells Log4Net that you want to create a new logger and
that it identified by the typeof(Form1) parameter. Which in this case is
LoggerTest.Form1, this tag will be used later during Log4Net configuration.
Each application will also need an additional line during startup that defines the configuration
of Log4Net which is as follows:
static void Main()
{
XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo(@"logConfig.XML"));
Application.Run(new Form1());
}
The line XmlConfigurator.ConfigureAndWatch, configures Log4Net by using the
logConfig.xml file and will watch the file for changes so that if the configuration file is
changed Log4Net will reconfigure itself based upon the new configuration information without
the need for a restart.
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 1 of 8
It is also useful to turn off logging when the application closes by calling the log4Net
shutdown methods. This allows all Appenders to be shut down correctly.
protected override void Dispose( bool disposing )
{
log.Warn(string.Format("Application ended {0}"
,System.DateTime.Now.ToString("dd-MMM-yyy hh:mm:ss")));
log4net.LogManager.Shutdown();
}
Using Log4Net is now fairly easy to use. There are 5 levels of logging:
Debug
Info
Warn
Error
Fatal
Configuration File
The Log4Net configuration file is XML based and is used to configure the Level of logging, the
areas to log, the types of logging required. A sample configuration is as follows:
<log4net>
<!-- A1 is set to be an OutputDebugStringAppender -->
<appender name="A1"
type="log4net.Appender.OutputDebugStringAppender">
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %C.%M - %m%n" />
</layout>
</appender>
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 2 of 8
<!-- Set root logger level to DEBUG and its appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
<log4net>
There are 2 parts to this file: The appender and the log level. In this example the appender is
set to be an OutputDebugStringAppender, which will log to DebugView. The log level is
set to DEBUG, which is the lowest level of logging and will also log all other levels. If the log
level was set to FATAL, only logs at the fatal level will be logged.
This log will produce a log in the following format:
DEBUG 28064 LoggerTest.Form1 - Form1 button1_Click : Checkbox checked
This format is defined by the conversionPattern parameter. The format options are as
follows:
Type
%c
Description
Used to output the category of the logging event. The category conversion specifier can
be optionally followed by precision specifier, that is a decimal constant in brackets.
If a precision specifier is given, then only the corresponding number of right most
components of the category name will be printed. By default the category name is
printed in full.
For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
%C
Used to output the fully qualified class name of the caller issuing the logging request.
This conversion specifier can be optionally followed by precision specifier, that is a
decimal constant in brackets.
If a precision specifier is given, then only the corresponding number of right most
components of the class name will be printed. By default the class name is output in
fully qualified form.
For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will
output "SomeClass".
WARNING Generating the caller class information is slow. Thus, it's use should be
avoided unless execution speed is not an issue.
%d
Used to output the date of the logging event. The date conversion specifier may be
followed by a date format specifier enclosed between braces.
For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date
format specifier is given then ISO8601 format is assumed.
%F
Used to output the file name where the logging request was issued.
WARNING Generating caller location information is extremely slow. It's use should be
avoided unless execution speed is not an issue.
%l
Used to output the line number from where the logging request was issued.
WARNING Generating caller location information is extremely slow. It's use should be
avoided unless execution speed is not an issue.
%m Used to output the application supplied message associated with the logging event.
%M
Used to output the method name where the logging request was issued.
WARNING Generating caller location information is extremely slow. It's use should be
avoided unless execution speed is not an issue.
%n
%p
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 3 of 8
%r
Used to output the number of milliseconds elapsed since the start of the application until
the creation of the logging event.
%t
Used to output the name of the thread that generated the logging event.
Version 1.0
http://www.recneps.co.uk
Page 4 of 8
This will log the LoggingCheck.Form2 class at INFO level and everything else at FATAL.
To log at the namespace level change LoggingCheck.Form2 to LoggingCheck.
Logging can also be directed to different appenders depending upon the logger used.
<logger name="LoggingCheck.Form1">
<level value="Warn" />
<appender-ref ref="RollingFile2" />
</logger>
Adding this logger will log LoggingCheck.Form1 to the RollingFile2 appender, which
will also need defining in your config file.
Note:
Logging
Logging
Logging
Logging
Logging
Logging
Logging
as
as
as
as
as
as
as
Configuration in App.Config/Web.Config
Using a separate config file for log4net has its advantages, especially if log4net is configured
to watch a config file. Using a separate config file means that log4net will only be
reconfigured when the log4net config file changes and not when other config is changed. The
log4net configuration can also be added to the web.config or app.config files.
Modify AssemblyInfo.cs and add
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
in web.config/app.config specify a new configSection
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"
/>
</configSections>
.
.
<log4net>
<!-- Add Log4Net Config Here -->
</ log4net >
</configuration>
If you are using log4net on a web application then the folder that contains the log files needs
to be have aspnet user write permissions.
Compact Framework
Log4Net supports the compact framework as a separate build. When a Smart Device
Application is created in Visual Studio, add in the reference to the netcf version of the dll.
The compact framework version does not support the following:
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 5 of 8
Types of Appender
Below is a list of Appenders that are available
(from http://logging.apache.org/log4net/release/manual/introduction.html)
Type
Description
log4net.Appender.ADONetAppender
log4net.Appender.ASPNetTraceAppender
log4net.Appender.BufferingForwardingAppender
log4net.Appender.ColoredConsoleAppender
log4net.Appender.ConsoleAppender
log4net.Appender.EventLogAppender
log4net.Appender.FileAppender
log4net.Appender.ForwardingAppender
log4net.Appender.MemoryAppender
log4net.Appender.NetSendAppender
log4net.Appender.OutputDebugStringAppender
log4net.Appender.RemotingAppender
log4net.Appender.RollingFileAppender
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 6 of 8
log4net.Appender.SMTPAppender
log4net.Appender.SmtpPickupDirAppender
log4net.Appender.TraceAppender
log4net.Appender.UdpAppender
/// <summary>
/// <param name="Url" value="http://www.yourwebservice/wsdlfile/" />
/// </summary>
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 7 of 8
Further Information
See http://sourceforge.net/projects/log4net
http://logging.apache.org/log4net/
Original Java implementation
http://logging.apache.org/log4j/docs/index.html
All ports
http://logging.apache.org/
Log4Net Overview
Recneps Limited 2007
Version 1.0
http://www.recneps.co.uk
Page 8 of 8