MailBee.NET Objects 4.0

Imap.LogNewEntry Event

Occurs when an entry is written into the log file (or into the memory buffer if Imap.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 IMAP4 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.ImapMail;

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;
            ((Imap)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)
    {
        Imap imp = new Imap();

        // Enable logging the POP3 session into a file
        imp.Log.Enabled = true;
        imp.Log.Filename = @"C:\Temp\log.txt";
        imp.Log.Clear();

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

        // Do something which would produce some network traffic
        imp.Connect("mail.domain.com");
        imp.Login("jdoe", "secret");
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Module Sample
    ' LogNewEntry event handler
    Private Sub OnLogNewEntry(ByVal sender As Object, ByVal e As LogNewEntryEventArgs)
        If Not e.NewEntry.MessageType = LogMessageType.Recv OrElse _
            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
            CType(sender, Imap).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
    Sub Main(ByVal args As String())
        Dim imp As New Imap

        ' Enable logging the POP3 session into a file
        imp.Log.Enabled = True
        imp.Log.Filename = "C:\Temp\log.txt"
        imp.Log.Clear()

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

        ' Do something which would produce some network traffic
        imp.Connect("mail.domain.com")
        imp.Login("jdoe", "secret")
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace