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);
}
Last active
September 24, 2019 11:10
-
-
Save lankymart/8867fb39c43ced877ec23333f339640d to your computer and use it in GitHub Desktop.
Log4Net Implementation for the MailKit IProtocolLogger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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