Logging frameworks are invaluable in application development. This log4net tutorial will look at it’s benefits as a logging framework, how to set it up and how to create entries.
Some logging frameworks, such as ELMAH and New Relic specialize in certain areas of logging. So, it’s worth considering your requirement carefully before selecting a framework. It’s possible that you may wish to use multiple frameworks.
We’ve all written our own logging frameworks at some point. After all, you can get pretty far with just writing to a text file. However, as in all development, it makes sense to use something that’s already been written—and, more importantly, tested—for you.
log4net is a popular general-purpose logging framework. It’s simple to use yet powerful. And, as it’s a well-known open-source project, it’s likely to be familiar to other developers and/or support staff who may need to use your logging system from time to time. At the very least they can find copious documentation and help online.
Installing and configuring log4net is one of the first things I do when writing a new application. It’s just handy to be able to log data at various stages of the development life cycle.
I’m going to give a brief introduction to setting up log4net within an application. There are numerous setup options, but we’ll go through a pretty common configuration.
Create a new console application and use NuGet to find and install the log4net
package.
Open up AssemblyInfo.cs
(under Properties
in the Solution Explorer view of your application). Add the following line to the end of the file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config",
Watch = true)]
This tells log4net that the configuration information (XML) is located in log4net.config
.
Create a new log4net.config
file in the root of the application and set its Copy to Output Directory
to be Copy if newer
. The new file should contain the following XML document
<?xml version="1.0"?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\Today.log" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<appendToFile value="true "/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %logger %date{ISO8601} - %message%newline" />
</layout>
<maxSizeRollBackups value="1" />
<maximumFileSize value="100MB" />
</appender>
<root>
<!-- Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and "OFF". -->
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Details of the options are available online, but the highlights are
C:\Logs\Today.log
. Files will be renamed when they rollover, so Today.log
is always the most recent.ALL
—the most verbose settingOne of the joys of using a third-party logging system is all the features you get for nothing. Once log4net is configured, logging to a database, the console, the Windows event log, e-mail, a UDP connection, etc. is as simple as defining a new appender in the configuration file.
And, with that, we are ready to go. So, how do we actually create a new log entry?
In a class you want to log from add the following field
private static readonly ILog Log = log4net.LogManager
.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Then, from a method in the class, you can add a DEBUG
entry using
this.Log.Debug("We are logging");
Want to add an INFO
entry? Then it’s
this.Log.Info("We are logging");
There are also very useful formatting helpers that use the same syntax as string.Format
this.Log.DebugFormat("We are logging entry #{0}", 3);
The resulting entry in C:\Logs\Today.log
will look like
DEBUG ConsoleApplication1.Program 2015-07-17 13:27:03,079 - We are logging entry #3
And that’s all there is to it. Well, actually, it’s far from all there is to it—there’s a lot more to log4net—but you should now be up and running.
I hope this log4net tutorial has been helpful. If you want to learn more about .NET development, Learning Tree has a comprehensive range of over 20 .NET courses covering all the major topics. Many of these are 1-day online sessions you can take from the convenience of home or office.