Using Logging in Custom Listeners

We covered recently Extending MicroLite with custom Listeners and as a result you may find that you create a Listener that could be distributed for used by other people.

Lets for example say that you created a Listener to perform validation and wanted to log any validation warnings and errors, you can utilise the logging abstraction provided by MicroLite.

Now since we cannot guarantee that there will be a logger present, we don’t want to call log directly as it may be null. There are a set of extension methods available which will check whether a logger is in use and you should call them instead. They are in the MicroLite.Logging namespace so if you add a using for MicroLite.Logging instead of fully qualifying the ILog and LogManager class they will show up in intellisense.

This example is to show you how you could log the warnings and errors (Validator and ValidationException are fictional classes used as an example).

Firstly, add a static field to the class which is of type MicroLite.Logging.ILog which is resolved by the MicroLite LogManager class.

Then call the TryLog.. extension methods so that we don’t have to check explicitly whether we have a logger or not.

using MicroLite.Logging;

public class ValidationListener : MicroLite.Listeners.Listener
    private static ILog log = LogManager.GetCurrentClassLog();

    protected override BeforeInsert(object instance)
        log.TryLogDebug("Validating an object before insert");

    protected override BeforeUpdate(object instance)
        log.TryLogDebug("Validating an object before update");

    private void ValidateObject(object instance)
        var warnings = Validator.GetWarnings(instance);
        var errors = Validator.GetErrors(instance);

        foreach (var warning in warnings)

        foreach (var error in errors)

        if (warnings.Count > 0 || errors.Count > 0)
            throw new ValidationException(warnings, errors);

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


3 thoughts on “Using Logging in Custom Listeners

  1. dcsteele

    Very useful – the Try.. methods will save a test every time. Just one question: what happens when I raise an exception ? Do any other [custom] Listeners execute ?

    1. trevorpilley Post author

      The listeners will be invoked in order, if any of them throw an exception then no others will be invoked and the Session will not continue.

      So for example, if we have 3 listeners and insert a new customer, BeforeInsert will be called on each listener in turn, if any of them throw an exception, none of the remaining listeners will be invoked and the customer WILL NOT be inserted into the database. The same applies for Update and Delete too.

  2. Pingback: MicroLite 5.0 – Changes to Logging | MicroLite ORM

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