Quick Start Log4Net

Alex van Buitenen, maart 2009

Log4Net is a open source logger.

This Quick start should get you up and running with Log4Net in a very short time.
It assumes you have Log4Net installed. If not, you can find it at http://logging.apache.org/log4net
Furthermore, basic knowledge about Visual Studio and C# is assumed.

This Quick Start does NOT duplicate any Log4Net documentation.
After your Quick Start using this document you should study the Log4Net documentation for more advanced usage.

Contents

Introduction

  1. Edit your app.config
  2. Enable configuration
  3. Start logging

Introduction

The documentation that comes with Log4Net does not contain a Quick Start.
So, as a first-time user you end up reading lots of stuff you're not interested in yet.
It is difficult to pinpoint the items you DO need right away to get up and running..

I wrote this Quick Start because I didn't remember how I used Log4Net in a previous project.
I found myself searching thru my previous projects, wondering where I'd used it.
Even after I had found it, it took some time before I had found how I had configured and used Log4Net the previous time.
So I wrote down the next three steps as a reminder.

1. Edit your app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </configSections>

    <log4net>
        <appender name="MyAppender" type="log4net.Appender.FileAppender">
            <file value="log-MyAppender.txt" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%d{dd-MM-yyyy HH:mm:ss} [%X{user}] - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="" />
        </root>
        <logger name="MyLogger">
            <level value="DEBUG" />
            <appender-ref ref="MyAppender" />
        </logger>
    </log4net>
</configuration>

Note on configsections

From: log4net Manual - Configuration

"In order to embed the configuration data in the .config file the section name must be identified to the .NET config file parser using a configSections element.
The section must specify the log4net.Config.Log4NetConfigurationSectionHandler that will be used to parse the config section.
This type must be fully assembly qualified because it is being loaded by the .NET config file parser not by log4net."

Note on Layout

For the layout in the Conversion pattern, see Log4Net SDK Reference - PatternLayout Class

Note on appender

For the different appenders and their configuration, see log4net Manual - Config Examples
 

2. Enable Configuration

Add a reference to log4net.dll in your project.
Tell Log4Net programmatically to use the app.config file to configure by calling
XmlConfigurator.Configure();

For example in Programs.cs:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net.Config;

namespace QuickStartLog4Net
{
   
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        ///</summary>

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            XmlConfigurator.Configure();
            Application.Run(new MainForm());
        }
    }
}

Note on Configuration

You can configure Log4Net in two ways:

 

3. Start Logging

use something like:

class MyClass
{
    private static readonly ILog _log = LogManager.GetLogger("MyLogger");

    public MyClass()
   
{
       
_log.Debug("logging from MyClass");
   
}
}


For more info, see the log4net Manual and Log4Net SDK Reference.