Saturday, May 12, 2012

Log4Net: Minimalist demo using config settings

The other day, it was decided at work that we will be using Log4Net, Apache’s logging tool for the .NET Framwork. It was left to me to investigate this tool. This post is the beginning of this investigation. For my first post, I present a minimal demo of Log4Net using Config settings. So here are the steps I’ve come up with using Microsoft C# Express 2010:

  1. Create a Console Application Project
  2. Make sure that Target Framework is .NET Framework 4, (select the project in Solution Explorer and press Alt+Enter, Look at Target Framework), the default is .NET Framework 4 Client Profile.
  3. Add a reference to log4net.dll
  4. Add the following attribute outside of any class or namespace (in my project I put it in Program.cs between the using and namespace statements, in a real project I would probably put it in AssemblyInfo.cs):
    [assembly: log4net.Config.XmlConfigurator]
    
    In this case XmlConfigurator tells the Framework to look in my application's .config file for configuration information.
  5. In app.config:

    <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections>

    This tells the framework about Log4Net’s custom configuration section below.

    <log4net> <appender name="MyAppender" type="log4net.Appender.FileAppender"> <file value="TestLog.txt"/> <layout type="log4net.Layout.SimpleLayout" /> </appender>


    log4net is the configuration section we described configSections above.


    This creates an appender; an appender is an object that is writes the logged data to somewhere. A FileAppender will write it to a text file. The name is used below to tell log4net to actually use this appender;


    The file element declares the name of the file. Here we will be logging to TestLog.txt in the same directory as the program.


    The layout tells log4net how to write the data. SimpleLayout is as simple as they get, In the real world, I’d use a more flexible layout. the PatternLayout is popular.

    <root>
    <appender-ref ref="MyAppender"/>
    </root>
    </log4net>

    Declaring an appender is good, but log4net won’t use it unless you tell it to.  The appender-ref element tells log4net to use my appender. You can specify as many appenders as you want.


  6. Create an instance of Log
    log4net.ILog log = log4net.LogManager.GetLogger("Some String");
    

  7. And Log away.
    log.Info("For Your Information");
    log.Warn("Danger Will Robinson");
    log.Debug("Debug Message");
    try
    {
    throw new Exception("Deliberate Exception");
    }
    catch (Exception ex)
    {
    log.Error(ex.Message, ex);
    }
    log.Fatal("Die. Die. Die!", new ApplicationException("Fatal Exception"));

    Running the code above, my log looks like this (note that there are not time stamps):

    INFO - For Your Information
    WARN - Danger Will Robinson
    DEBUG - Debug Message
    ERROR - Deliberate Exception
    System.Exception: Deliberate Exception
    at Log4NetConsole001.Program.Main(String[] args) in C:\Users\jack\documents\visual studio 2010\Projects\Log4NetConsole001\Log4NetConsole001\Program.cs:line 20
    FATAL - Die. Die. Die!
    System.ApplicationException: Fatal Exception
    
    


 

That is my minimalist implementation of Log4Net; it is probably too simple to be useful, but is is a start. If my ambition holds, I hope to expand on this.

Complete Sample

program.cs

using System;

[assembly: log4net.Config.XmlConfigurator]

namespace Log4NetConsole001
{
class Program
{
static void Main(string[] args)
{
log4net.ILog log = log4net.LogManager.GetLogger("Some String");
log.Info("For Your Information");
log.Warn("Danger Will Robinson");
log.Debug("Debug Message");
try
{
throw new Exception("Deliberate Exception");
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
log.Fatal("Die. Die. Die!", new ApplicationException("Fatal Exception"));
Console.WriteLine("======================= End ========================");
Console.ReadKey();
}
}
}

app.config

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

<log4net>
  <appender name="MyAppender" type="log4net.Appender.FileAppender">
<file value="TestLog.txt"/>
<layout type="log4net.Layout.SimpleLayout" />
  </appender>

<root>
<appender-ref ref="MyAppender"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

No comments: