MailBee.NET Objects 4.0

Smtp.LogNewEntry Event

Occurs when an entry is written into the log file (or into the memory buffer if Smtp.Log.MemoryLog is true).

public event LogNewEntryEventHandler LogNewEntry;

Event Data

The event handler receives an argument of type LogNewEntryEventArgs containing data related to this event. The following LogNewEntryEventArgs properties provide information specific to this event.

Property Description
NewEntry Gets a reference to the log entry MailBee is going to put into the log file (or into the memory buffer if MemoryLog is true).
State Gets a reference to the object which was supplied by the developer in state parameter of asynchronous methods of the mailer components.

Remarks

This event is raised only if logging is turned on (Enabled is true).

MailBee raises this event BEFORE adding the entry into the the log. Since many properties of NewEntry object are editable, the developer can change the data MailBee will put into the log or even cancel logging the current entry at all. This may be useful the developer wishes to log only those entires which meet certain criteria.

Note   Entries added by the developer (through WriteLine method call) do not cause this event to be raised. Thus, it's safe to add user-defined log entries even in the LogNewEntry event handler itself. This does not cause endless recursion which would otherwise occur if WriteLine method called in LogNewEntry event handler raised this event again.

Example

This sample enables logging the SMTP session into a file, and logs only those entries which indicate any data traffic between the client (MailBee) and the server. Other entries are discarded, and user-defined log entry is added instead. The sample is written for a console application.

[C#]
using System;
using MailBee;
using MailBee.SmtpMail;

class Sample
{
    // LogNewEntry event handler.
    private static void OnLogNewEntry(object sender, LogNewEntryEventArgs e)
    {
        if ( !(e.NewEntry.MessageType == LogMessageType.Recv ||
            e.NewEntry.MessageType == LogMessageType.Send) )
        {
            // Do not add this entry if it does not indicate any data transfer,
            // and put our own message instead.
            e.NewEntry.AddThisEntry = false;
            ((Smtp)sender).Log.WriteLine("UNWANTED MESSAGE REMOVED");

            // Note: we could produce similar results just by writing new values 
            // into MessageText and MessageComment properties of the original entry.
            // The code above uses Log.WriteLine method just to show how to add new 
            // entries in the event handler itself. You may comment 2 code lines above 
            // and uncomment 2 code lines below to use the alternate approach.
            //
            // e.NewEntry.MessageText = "UNWANTED MESSAGE REMOVED";
            // e.NewEntry.MessageComment = string.Empty;
        }
    }

    // The actual code.
    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        mailer.SmtpServers.Add("smtp.domain.com");

        // Enable logging the SMTP session into a file.
        mailer.Log.Enabled = true;
        mailer.Log.Filename = @"C:\Temp\log.txt";
        mailer.Log.Format = LogFormatOptions.AddContextInfo;
        mailer.Log.Clear();

        // Subscribe to the LogNewEntry event.
        mailer.LogNewEntry += new LogNewEntryEventHandler(OnLogNewEntry);

        // Do something which would produce some network traffic.
        mailer.Connect();
        mailer.Hello();
        mailer.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.SmtpMail

Class Sample
    ' LogNewEntry event handler
    Private Shared Sub OnLogNewEntry(ByVal sender As Object, ByVal e As LogNewEntryEventArgs)
        ' Check whether the error is critical or not.
        If e.NewEntry.MessageType <> LogMessageType.Recv And e.NewEntry.MessageType <> LogMessageType.Send Then
            ' Do not add this entry if it does not indicate any data transfer,
            ' and put our own message instead.
            e.NewEntry.AddThisEntry = False
            sender.Log.WriteLine("UNWANTED MESSAGE REMOVED")

            ' Note: we could produce similar results just by writing new values 
            ' into MessageText and MessageComment properties of the original entry.
            ' The code above uses Log.WriteLine method just to show how to add new 
            ' entries in the event handler itself. You may comment 2 code lines above 
            ' and uncomment 2 code lines below to use the alternate approach.
            '
            ' e.NewEntry.MessageText = "UNWANTED MESSAGE REMOVED"
            ' e.NewEntry.MessageComment = String.Empty
        End If
    End Sub

    ' The actual code
    Shared Sub Main(ByVal args As String())
        Dim mailer As New Smtp

        mailer.SmtpServers.Add("smtp.domain.com")

        ' Enable logging the SMTP session into a file.
        mailer.Log.Enabled = True
        mailer.Log.Filename = "C:\Temp\log.txt"
        mailer.Log.Format = LogFormatOptions.AddContextInfo
        mailer.Log.Clear()

        ' Subscribe to the LogNewEntry event.
        AddHandler mailer.LogNewEntry, AddressOf OnLogNewEntry

        ' Do something which would produce some network traffic.
        mailer.Connect()
        mailer.Hello()
        mailer.Disconnect()
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace