Sitecore Custom Logging

Every Sitecore Developer faces a situation in which he will need to build a custom logging mechanism. One of the most common scenarios would be a multi-site solution in which the standard log will become huge and finding the exact trace information becomes painful.

On the bright side Sitecore built-in logging uses log4net and offers different ways to extend the logging process. In this post I will cover the easiest approach for dealing with custom logs.

In the web.config just beneath the <sitecore> node you can find the log4net configuration section. After examining it you will notice that you must do two things in order to create a new logger:

    • create a new appender that will append to a file of your choice
    • register a new logger that is going to use this appender and can be referenced in the code later on.

So here are the 3 easy steps to get your brand new logger up and running.

Step 1 – Configuring a custom appender

Configuring the custom appender is as easy as copying and pasting one of the existing sections and modifying it to suit the new logger`s needs.


 <appender name="MyCustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
 <file value="$(dataFolder)/logs/mycustomlogfile.{date}.txt" />
 <appendToFile value="true" />
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
 </layout>
 <encoding value="utf-8" />
 </appender>

You can notice that it almost entirely reuses the standard LogFileAppender. The two changes are the appender name and the name of the file it will append to.

Step 2 – Configuring a custom logger

Configuring the custom logger is as simple as configuring the custom appender.


 <logger name="Sitecore.Diagnostics.MyCustomLogger" additivity="false">
 <level value="INFO" />
 <appender-ref ref="MyCustomLogFileAppender" />
 </logger>

Just make sure that the appender-ref matches your custom appender.

Step 3 – Creating a wrapper class to work with your custom logger

When dealing with custom logger it will be nice to have a way to log in the same manner as we do with the Sitecore.Diagnostics.Log. For this purpose we can build a class that covers all the functionality that the Sitecore.Diagnostics.Log offers.


using log4net;
using Sitecore.Diagnostics;
using System;

namespace Sandbox.Diagnostics
{
public static class Log
{
private static readonly ILog _logger = log4net.LogManager.GetLogger("Sitecore.Diagnostics.MyCustomLogger");

public static void Debug(string message)
{
Assert.ArgumentNotNull(message, "message");

_logger.Debug(message);
}

public static void Debug(string message, Exception exception)
{
Assert.ArgumentNotNull(message, "message");
Assert.ArgumentNotNull(exception, "exception");

_logger.Debug(message, exception);
}

public static void Info(string message)
{
Assert.ArgumentNotNull(message, "message");

_logger.Info(message);
}

public static void Info(string message, Exception exception)
{
Assert.ArgumentNotNull(message, "message");
Assert.ArgumentNotNull(exception, "exception");

_logger.Info(message, exception);
}

public static void Warn(string message)
{
Assert.ArgumentNotNull(message, "message");

_logger.Warn(message);
}

public static void Warn(string message, Exception exception)
{
Assert.ArgumentNotNull(message, "message");
Assert.ArgumentNotNull(exception, "exception");

_logger.Warn(message, exception);
}

public static void Error(string message)
{
Assert.ArgumentNotNull(message, "message");

_logger.Error(message);
}

public static void Error(string message, Exception exception)
{
Assert.ArgumentNotNull(message, "message");
Assert.ArgumentNotNull(exception, "exception");

_logger.Error(message, exception);
}

public static void Fatal(string message)
{
Assert.ArgumentNotNull(message, "message");

_logger.Fatal(message);
}

public static void Fatal(string message, Exception exception)
{
Assert.ArgumentNotNull(message, "message");
Assert.ArgumentNotNull(exception, "exception");

_logger.Fatal(message, exception);
}
}
}

Please keep in mind that for using the log4net functionality you will need to add reference to the Sitecore.Logging assembly in your project.

For more complex implementations you can refer to Alex Shyba`s Chain on the topic.

Hope this helps and happy logging !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s