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:

  1. 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.
  2. Add a reference to the dll in your project solution:
    • References
  3. Add the following code to the Assembly.cs file.  More documentation here:
    • // Configure log4net using the .config file
      [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.
  4. Add the following section to the web/app.config file in the <configuration> node:
    • <configSections>
       
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </
      configSections>
  5. Create a new section in the web/app.config using log4net as the node name:
    • <log4net>
       
      <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>
  6. At the top of the class in which you want to implement logging, add the using log4net; directive.
  7. Define a static logger variable at the top of your class.  Something like this will work:
    • private static readonly ILog log = LogManager.GetLogger(typeof(Program));
  8. Start adding logging statements to your code, for example:
    • log.Info("Entering application.");
  9. Altogether then, your class might look something like this:
    • using System;
      using System.Collections.Generic;
      using System.Text;
      using log4net; </p>

      </font>namespace log4netDemo
      {
         
      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>

              </font>static void Main(string[] args)
             
      {
                 
      log.Info("Entering application."); </p>

                  </font>for (int i = 0; i < 10; i++)
                 
      {
                     
      log.DebugFormat("Inside of the loop (i = {0})", i);
                 
      } </p>

                  </font>log.Info("Exiting application.");
             
      }
         
      }
      } </div> </ul>

    • Which would produce this output when run:
      • 2008-05-12 19:53:28,059 [11] INFO  log4netDemo.Program [(null)] – Entering application.
        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 = 8)
        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.
      </ol>

       

      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.