Skip to content

Instantly share code, notes, and snippets.

@lankymart
Last active September 24, 2019 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lankymart/8867fb39c43ced877ec23333f339640d to your computer and use it in GitHub Desktop.
Save lankymart/8867fb39c43ced877ec23333f339640d to your computer and use it in GitHub Desktop.
Log4Net Implementation for the MailKit IProtocolLogger

Log4Net Implementation for the MailKit IProtocolLogger

Usage

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
...
using (var client = new SmtpClient (new Log4NetLogger(Log)) 
{
    client.Connect ("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
    client.Authenticate ("username", "password");
    client.Send (message);
    client.Disconnect (true);
}
using log4net;
using log4net.Config;
using MailKit;
using System;
using System.Text;
public class Log4NetLogger : IProtocolLogger
{
static readonly string clientPrefix = "C:";
static readonly string serverPrefix = "S:";
private ILog Logger { get; set; }
public Log4NetLogger(ILog logger)
{
XmlConfigurator.Configure();
Logger = logger;
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
public void LogClient(byte[] buffer, int offset, int count)
{
Logger.InfoFormat("{0}\n{1}", clientPrefix, Encoding.UTF8.GetString(buffer, offset, count));
}
public void LogConnect(Uri uri)
{
Logger.InfoFormat("Connected to {0}", uri);
}
public void LogServer(byte[] buffer, int offset, int count)
{
Logger.InfoFormat("{0}\n{1}", serverPrefix, Encoding.UTF8.GetString(buffer, offset, count));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment