Creating a custom logging extension

As you will have seen in the Logging post, there are currently extension packages available to enable MicroLite to write to log4net or NLog. While these are the most popular logging frameworks, there are others available or you may use an in house logging solution.

If you do not use log4net or NLog, you can easily create your own extension for logging.

Start by creating a new class library project, the assembly naming convention used is MicroLite.Extensions.X where X is the functionality provider e.g. MicroLite.Extensions.Log4Net – log4net implementation of logging.

Then create a class which implements the MicroLite.Logging.ILog interface, the log4net and NLog extensions simply wrap the logger in that framework and forward the method call. However, depending on the logging framework you are using, you may have to do more than that.

internal class LogAdapter : MicroLite.Logging.ILog
    // implement interface as required.

Then, we need to add a class called ConfigurationExtensions in the MicroLite.Configuration namespace which extends IConfigureExtensions. The method name should describe the extension (e.g. WithLog4Net). The reason to put the class in the MicroLite.Configuration namespace is to make the extension visible without having to specify an additional namespace since the application startup will already be referencing MicroLite.Configuration.

namespace MicroLite.Configuration
    public static class ConfigurationExtensions
          public static IConfigureExtensions WithX(this IConfigureExtensions configureExtensions)
               // Implement logic here.

               return configureExtensions;

The IConfigureExtensions interface exposes a number of methods to integrate with MicroLite, however we are only interested in SetLogResolver at this time. As you can see from the method signature, it simply requires a delegate which can resolve a string into an implementation of the MicroLite.Logging.ILog interface.

void SetLogResolver(Func<string, ILog> logResolver);

We then need to call the SetLogResolver method, this is an example of how the log4net implementation works:

public static IConfigureExtensions WithLog4Net(this IConfigureExtensions configureExtensions)
    configureExtensions.SetLogResolver((string name) =>
            var logger = log4net.LogManager.GetLogger(name);

            return new LogAdapter(logger);

    return configureExtensions;

The code for the extensions is all open source and on GitHub if you want to see the full implementation details.

This article was written for version 2.0.0 of the MicroLite framework.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s