A log4net appender that uses SmtpClient

Google Apps provides free email hosting for small businesses.  However, their SMTP server requires SSL authentication for sending outbound emails, so you can't use use log4net to send emails based on the content of logged messages.  Ron Grabowski suggested writing a log4net appender that uses SmtpClient (only available in 2.0) to send SSL secured messages. The below class works for me against smtp.google.com using port 587:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using log4net.Appender;

using log4net.Core;

using System.Net.Mail;

using System.Net;

namespace log4netExtensions

{

  /// <summary>

  /// The standard log4net SmtpAppender doesn't support SSL authentication, which is

  /// required to send email via gmail.

  ///

  /// This appender uses the SmtpClient (only available in .NET 2.0) to send SMTP mail that

  /// is secured via SSL.  This is needed to talk to the gmail SMTP server. 

  ///

  /// This code is heavily based on that posted by Ron Grabowski at:

  /// http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200602.mbox/%3C20060216123155.22007.qmail@web32202.mail.mud.yahoo.com%3E

  /// </summary>

  public class SmtpClientSmtpAppender : SmtpAppender

  {

    override protected void SendBuffer(LoggingEvent[] events)

    {

      try

      {

        StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

        string t = Layout.Header;

        if (t != null)

        {

          writer.Write(t);

        }

        for (int i = 0; i < events.Length; i++)

        {

          // Render the event and append the text to the buffer

          RenderLoggingEvent(writer, events[i]);

        }

        t = Layout.Footer;

        if (t != null)

        {

          writer.Write(t);

        }

        // Use SmtpClient so we can use SSL.

        SmtpClient client = new SmtpClient(SmtpHost, Port);

        client.EnableSsl = true;

        client.Credentials = new NetworkCredential(Username, Password);

        string messageText = writer.ToString();

        MailMessage mail = new MailMessage(From, To, Subject, messageText);

        client.Send(mail);

      }

      catch (Exception e)

      {

        ErrorHandler.Error("Error occurred while sending e-mail notification from SmtpClientSmtpAppender.", e);

      }

    }

  }

}

Published 20 April 2007 05:36 PM by Ted_Graham
Filed under: ,

Comments

# Ted Graham on .NET : A log4net appender that uses SmtpClient said on 20 April, 2007 06:21 PM

PingBack from http://weblogs.asp.net/tgraham/archive/2007/04/20/a-log4net-appender-that-uses-smtpclient.aspx

# A log4net appender that uses SmtpClient said on 26 November, 2007 12:59 PM

Pingback from  A log4net appender that uses SmtpClient

# Mani said on 08 January, 2008 07:48 AM

very useful! thank u very much.

# alfred said on 02 June, 2009 11:32 PM

how to use the class? what changes need to be done on App.config

# Twitter Trackbacks for A log4net appender that uses SmtpClient - Ted Graham on .NET [asp.net] on Topsy.com said on 04 May, 2010 03:23 PM

Pingback from  Twitter Trackbacks for                 A log4net appender that uses SmtpClient - Ted Graham on .NET         [asp.net]        on Topsy.com

# pushkar said on 14 July, 2010 06:32 AM

Hello Graham.

Thanks for nice solution.

But can you please elaborate how to implement this extension class in asp.net ???

Will it work If i just make this SmtpClientSmtpAppender class in my App_Code folder ?

Please reply.

Thanks in Advance

Pushkar

# Marcus said on 19 October, 2010 06:23 AM

Very nice, thanks alot :)

# Mick said on 12 December, 2010 08:11 PM

Has support been added to log4net for SSL?  I can see enablessl as a part of the config for the SmtpAppender

# hooher tod said on 04 September, 2011 09:21 PM

Yes there should realize the reader to RSS my feed to RSS commentary, quite simply

Leave a Comment

(required) 
(required) 
(optional)
(required)