Quick way of emailing all exceptions that occur in an application

Exceptions are very common to occur. It would be great if we can handle such things in a common place. In an ASP.Net application, Application events serve this very purpose. But we should be careful in applying these events, as these are "Application level" and could impact the entire application.

A good way of handling a website exception is to mail it to the Administrator. 
The easiest way is to trap it in the Application level event (Application_Error event)
and send an email with the exception Message

///Handles all errors at the application level.
void Application_Error(Object sender, EventArgs e) { Exception ex    = Server.GetLastError(); MailMessage msgMail = new MailMessage();
//Make this configurable in web.config
msgMail.From = "autoadmin@sms.com";

//Make this configurable in web.config
msgMail.To = "admin@sms.com";

msgMail.Subject = ex.Message.ToString (); msgMail.Body = ex.ToString (); msgMail.BodyFormat = MailFormat.Html;

//Make this configurable in web.config

SmtpmsgMail.SmtpServer = "localhost"; SmtpmsgMail.Send( mail ); }
By [)ia6l0 iii   Popularity  (688 Views)