For our projects, we use an httpmodule to handle all unhandled exceptions. This module will log a lot of useful information such as exception info, user info and web server info into database automatically. And we could then view or search all this information daily to find out if there is a security problem, performance problem or bug. Demo code of httpmodule is:
public class ExceptionModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.Error += new EventHandler(OnError);
}
public void OnError(object sender, EventArgs args)
{
HttpApplication application = (HttpApplication)sender;
te_ExceptionLog log = GetExceptionLog(application.Server.GetLastError(), application.Context); // form exception entity in another method
string ExceptionProcessWay = ConfigurationManager.AppSettings["ExceptionProcessWay"]; // how to handle exception? log into db or send mail?
IExceptionProcess server = (IExceptionProcess)Activator.GetObject(typeof(IExceptionProcess), ConfigurationManager.AppSettings["ETSRemotingURL"].ToString());
server.LogException(log, ExceptionProcessWay); // we use .net remoting to access database, you can directly use ado.net here
}
}
Should be aware that, you still need to use customErrors configuration to direct user to a friendly page.
BTW, you can also have a look at ASP.NET 2.0 health monitoring.
发email:
Dim Mail As MailMessage = New MailMessage(dr("EMailFrom").ToString, dr("EMailTo").ToString)
Mail.Subject = dr("EMailSubject")
Mail.Body = dr("ExceptEMailBody")
Dim SMTP As SmtpClient = New SmtpClient()
SMTP.Host = dr("EMailSMTP")
SMTP.Credentials = New System.Net.NetworkCredential(dr("EMailAccount"), dr("EMailPassword"))
SMTP.Send(Mail)