Quick and easy log4net setup
I recently wanted to set up log4net on a project I’ve been working on and found the documentation somewhat lacking when trying to find a quick and easy setup guide. So, once I had everything set up and working, I resolved to create one myself.
So, in less time than it takes to get a pizza delivered, here is how to quickly get log4net setup on your project.
What you’ll need:
- The log4net dll (you can find the most recent version here)
- A .Net project/solution that you’d like to add logging to (you’re on your own here)
- Access to the Assembly.cs file for said project
- Access to the app.config or web.config file for said project
- A place to store your logging files (I will be using C:\ in this example, but this is one area in which the documentation actually excels)
Implementation:
- Place the log4net dll somewhere that you can reference it easily. I like to keep a Libraries folder around in most of my projects to hold any 3rd party dlls, but the choice is up to you.
- Add a reference to the dll in your project solution:
- Add the following code to the Assembly.cs file. More documentation here:
- Add the following section to the web/app.config file in the <configuration> node:
- Create a new section in the web/app.config using log4net as the node name:
- At the top of the class in which you want to implement logging, add the using log4net; directive.
- Define a static logger variable at the top of your class. Something like this will work:
- Start adding logging statements to your code, for example:
- Altogether then, your class might look something like this:
- Which would produce this output when run:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
using System.Collections.Generic;
using System.Text;
using log4net; </p>
</font>namespace log4netDemo </font>static void Main(string[] args) </font>for (int i = 0; i < 10; i++) </font>log.Info("Exiting application."); That really should be all there is to it. Although I did find the lack of a simple, quickstart guide daunting, there is actually a lot of good documentation on the log4net site. Particularly, if you want to use a different appender or read a more general introduction. If you’re interested, I’ve zipped up a sample solution using the code above. You can download it here.
{
class Program
{
// Define a static logger variable so that it references the name of your class
private static readonly ILog log = LogManager.GetLogger(typeof(Program)); </p>
{
log.Info("Entering application."); </p>
{
log.DebugFormat("Inside of the loop (i = {0})", i);
} </p>
}
}
} </div>
</ul>
</ol>
2008-05-12 19:53:28,260 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 0)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 1)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 2)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 3)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 4)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 5)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 6)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 7)
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i =
2008-05-12 19:53:28,280 [11] DEBUG log4netDemo.Program [(null)] – Inside of the loop (i = 9)
2008-05-12 19:53:28,280 [11] INFO log4netDemo.Program [(null)] – Exiting application.