Monday, December 8, 2008

Elmah Error Logging and Secure SMTP

[EDIT: As of 1.0, Elmah natively supports ssl with the useSsl property making the below tweaks unneccessary.]

For those of you who don't know elmah, check it out. Elmah is s a great, open source tool for logging all the errors on your web application.

One enhancement I have made to the source is adding secure smtp capability. This is important for those of us who like to use Google hosted email, which only rides on secure protocols.

The changes are quite simple. The only class you need to modify is the ErrorMailModule. There are three steps to modifying the source:

1. Add a property (I've called mine SmtpSecure).
2. Load the property from the config in OnInit.
3. Enable ssl on the SmtpClient in the SendMail function.

public class ErrorMailModule : HttpModuleBase, IExceptionFiltering
// Add the SmtpSecure property

private bool _smtpSecure;
protected virtual bool SmtpSecure
get { return _smtpSecure; }

protected override void OnInit(HttpApplication application)
// initialize the property
// this should be done in two steps for error trapping reasons

bool smtpSecure = Convert.ToBoolean(GetSetting(config, "smtpSecure", bool.FalseString));

// ... after the error handlers are attached

_smtpSecure = smtpSecure;


// Enable ssl on the smtp client in SendMail
protected virtual void SendMail(MailMessage mail)
// ... after client is instantiated

if (SmtpSecure)
client.EnableSsl = true;


That's it! The last thing to do is add the attribute in your web.config:

subject="Unhandled Exception"
smtpSecure="true" />

No comments: