More .NET components

Communication logging

Logging communication using LogWriter #

Communication logs are very useful when troubleshooting issues at the SMTP/IMAP/POP3/EWS protocols on communication level. Whenever you run into a problem, a log makes it possible to see what is going on.

To start logging, just add the following line into your code (just after creating the Smtp, Imap, Pop3 or Ews object):

CSharp

// create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
// ...

// start logging to a file
client.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt");

VisualBasic

' create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
' ...

' start logging to a file
client.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt")

A sample SMTP log file :

2015-11-03 17:08:51.150 Opening log file.
2015-11-03 17:08:51.150 Using FileLogWriter version 2.0.0.0.
2015-11-03 17:08:51.156 INFO Smtp(1)[6] Info: Connecting to smtpserver.local:25 using Smtp 2.0.0.0.
2015-11-03 17:08:51.279 INFO Smtp(1)[6] Response: 220 smtpserver.local Microsoft ESMTP MAIL Service ready at Fri, 3 Oct 2014 17:08:50 +0200
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Command: EHLO myworkstation.local
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-wsmtpserver.local Hello [192.168.1.2]
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-SIZE
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-PIPELINING
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-DSN
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-ENHANCEDSTATUSCODES
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-STARTTLS
2015-11-03 17:08:51.284 INFO Smtp(1)[6] Response: 250-X-ANONYMOUSTLS
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-AUTH NTLM LOGIN
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-X-EXPS GSSAPI NTLM
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-8BITMIME
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-BINARYMIME
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-CHUNKING
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-XEXCH50
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250-XRDST
2015-11-03 17:08:51.285 INFO Smtp(1)[6] Response: 250 XSHADOW
2015-11-03 17:08:51.300 INFO Smtp(1)[6] Command: QUIT

A sample IMAP log file :

2015-11-03 17:11:25.702 Opening log file.
2015-11-03 17:11:25.702 Using FileLogWriter version 2.0.0.0.
2015-11-03 17:11:25.708 INFO Imap(1)[6] Info: Connecting to imapserver.local:143 using Imap 2.0.0.0.
2015-11-03 17:11:25.834 INFO Imap(1)[6] Response: * OK The Microsoft Exchange IMAP4 service is ready.
2015-11-03 17:11:25.852 INFO Imap(1)[6] Command: R00001 CAPABILITY
2015-11-03 17:11:25.852 INFO Imap(1)[6] Response: * CAPABILITY IMAP4 IMAP4rev1 AUTH=GSSAPI AUTH=PLAIN STARTTLS IDLE NAMESPACE LITERAL+
2015-11-03 17:11:25.852 INFO Imap(1)[6] Response: R00001 OK CAPABILITY completed.
2015-11-03 17:11:25.855 INFO Imap(1)[6] Command: R00002 LOGOUT
2015-11-03 17:11:25.855 INFO Imap(1)[6] Response: * BYE Microsoft Exchange Server 2010 IMAP4 server signing off.
2015-11-03 17:11:25.855 INFO Imap(1)[6] Response: R00002 OK LOGOUT completed.

A sample POP3 log file :

2015-11-03 17:12:12.692 Opening log file.
2015-11-03 17:12:12.692 Using FileLogWriter version 2.0.0.0.
2015-11-03 17:12:12.698 INFO Pop3(1)[6] Info: Connecting to pop3server.local:110 using Pop3 2.0.0.0.
2015-11-03 17:12:12.823 INFO Pop3(1)[6] Response: +OK The Microsoft Exchange POP3 service is ready.
2015-11-03 17:12:12.830 INFO Pop3(1)[6] Command: CAPA
2015-11-03 17:12:12.830 INFO Pop3(1)[6] Response: +OK
2015-11-03 17:12:12.841 INFO Pop3(1)[6] Command: QUIT
2015-11-03 17:12:12.842 INFO Pop3(1)[6] Response: +OK Microsoft Exchange Server 2010 POP3 server signing off.

A sample EWS log file :

2015-11-03 17:12:58.158 Opening log file.
2015-11-03 17:12:58.158 Using FileLogWriter version 2.0.0.0.
2015-11-03 17:12:58.159 INFO Ews(2)[8] HTTP: Connecting to outlook.office365.com:443 using HttpClient 1.0.0.0.
2015-11-03 17:12:58.395 INFO Ews(2)[8] TLS: State StateChange:Negotiating
2015-11-03 17:12:58.481 INFO Ews(2)[8] TLS: State StateChange:Secured
2015-11-03 17:12:58.481 INFO Ews(2)[8] TLS: Connection secured using cipher: TLS 1.1, RSA, 256bit AES in CBC mode, SHA1
2015-11-03 17:12:58.481 INFO Ews(2)[8] HTTP: Sending request: HEAD /EWS/Services.wsdl
2015-11-03 17:12:58.515 INFO Ews(2)[8] HTTP: Received response: 401 Unauthorized.
2015-11-03 17:12:58.515 INFO Ews(2)[8] TLS: Alert Alert:Alert was sent.
2015-11-03 17:12:58.515 INFO Ews(2)[8] TLS: State StateChange:Closed

Log verbosity levels #

There are several levels of log verbosity:

  • Error - logs error messages only
  • Info - default level; logs important informative messages as well
  • Debug - logs all messages useful for debugging purposes
  • Verbose - very detailed log for advanced analysis; logs content of communication packets
    Warning: At the Verbose level, user credentials are written to the log as well.

To specify a log level, just pass an argument to FileLogWriter's constructor:

CSharp

// create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
// ...

// start logging to a file at debug level
client.LogWriter = new Rebex.FileLogWriter(
    @"C:\MyData\log.txt", Rebex.LogLevel.Debug);

VisualBasic

' create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
' ...

' start logging to a file at debug level
client.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt", Rebex.LogLevel.Debug)

Built-in log writers #

Text file

To log into a text file, use FileLogWriter:

CSharp

// create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
// ...

// start logging to a file at debug level
client.LogWriter = new Rebex.FileLogWriter(
    @"C:\MyData\log.txt", Rebex.LogLevel.Debug);

VisualBasic

' create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
' ...

' start logging to a file at debug level
client.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt", Rebex.LogLevel.Debug)

.NET trace listener

To log to .NET Trace Listeners, use TraceLogWriter:

CSharp

// create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
// ...

// start logging to subscribed trace listeners
client.LogWriter = new Rebex.TraceLogWriter(Rebex.LogLevel.Debug);

VisualBasic

' create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
' ...

' start logging to subscribed trace listeners
client.LogWriter = New Rebex.TraceLogWriter(Rebex.LogLevel.Debug)

Standard output stream

To log to the standard output stream, use ConsoleLogWriter:

CSharp

// create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
// ...

// start logging to the standard output stream at debug level
client.LogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);

VisualBasic

' create client instance (Rebex.Net.Smtp/Imap/Pop3/Ews)
' ...

' start logging to the standard output stream at debug level
client.LogWriter = New Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug)

Logging to multiple log writers

To log to more log writers at the same time, use TeeLogWriter:

CSharp

var consoleLogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);
var fileLogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt", Rebex.LogLevel.Info);

// start logging to both console and file log writers
client.LogWriter = new Rebex.TeeLogWriter(consoleLogWriter, fileLogWriter);

VisualBasic

Dim consoleLogWriter = New Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug)
Dim fileLogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt", Rebex.LogLevel.Debug)

' start logging to both console and file log writers
client.LogWriter = New Rebex.TeeLogWriter(consoleLogWriter, fileLogWriter)

Writing custom log writers #

To implement your own LogWriter, either implement Rebex.ILogWriter interface or simply derive a class from Rebex.LogWriterBase:

CSharp

// Sample log writer that logs to the standard error output stream
public class MyLogWriter : LogWriterBase
{
    protected override void WriteMessage(string message)
    {
        Console.Error.WriteLine("Rebex: {0}", message);
    }
}

VisualBasic

' Sample log writer that logs to the standard error output stream
Public Class MyLogWriter
    Inherits LogWriterBase
    Protected Overrides Sub WriteMessage(message As String)
        Console.Error.WriteLine("Rebex: {0}", message)
    End Sub
End Class