blog
twitter
Termite Logging Framework for .NET termite VS Tool online logging
 
tweet tweet tweet tweet tweet

Creating a Custom Logger

The framework provides a very easy way for you to log to anything you choose. It starts by subclassing Logger. From there you have two basic choices.

If your needs are to simply write the LogEntry as a String to some device, then you can just override the method WriteToLog(String). If your needs are more sophisticated, then you can override the method DoLog(LogEntry). In either case, the methods should return true if they were successful, and false otherwise.

Suppose you wanted to make a logger that simply displayed a message box to the user. Your code might look something like the following.

using System;
using System.Windows.Forms;
using BitFactory.Logging;

namespace My_Application
{
    public class MessageBoxLogger : Logger
    {
        protected override bool WriteToLog(String aString)
        {
            MessageBox.Show(aString);
            return true;
        }
    }
}

If you wanted something a little fancier, instead of overriding WriteToLog, you could instead override DoLog as in the following.

protected override bool DoLog(LogEntry aLogEntry)
{
    MessageBoxIcon icon = MessageBoxIcon.Warning;
    if (aLogEntry.Severity < LogSeverity.Warning)
        icon = MessageBoxIcon.Information;
    else if (aLogEntry.Severity > LogSeverity.Warning)
        icon = MessageBoxIcon.Error;

    MessageBox.Show(
        aLogEntry.Message,
        aLogEntry.SeverityString,
        MessageBoxButtons.OK,
        icon);

    return true;
}