Click or drag to resize

Custom ILogListener

If you want to create your own implementation of the ILogListener (for example, when you want to save messages in the database), the class LogListener can be inherited or the ILogListener interface can be implemented directly. The object of the LogMessage class is passed through the ILogListenerWriteMessages method. This class contains the information about the message source LogMessageSource ( for example, a strategy that has generated the message), the message type LogMessageLevel (information, warning or error), as well as the LogMessageMessage. text itself. The following example shows the source code of EmailLogListener:

C#
/// <summary>
public class EmailLogListener : LogListener
{

    public EmailLogListener(string from, string to)
    {
        if (from.IsEmpty())
            throw new ArgumentNullException("from");

        if (to.IsEmpty())
            throw new ArgumentNullException("to");

        From = from;
        To = to;
    }


    public string From { get; private set; }


    public string To { get; private set; }


    protected override void OnWriteMessage(LogMessage message)
    {
        var email = new SmtpClient();
        email.Send(new MailMessage(From, To, message.Source.Name + " " + message.Level, message.Message));
    }
}