I have used the following bit of code to diagnose numerous errors on websites and learn about others which were unknown to me. This is an ASP.NET 2.0 version, but you could modify it to work with v1.x by changing the email/smtp portion. I have seen other similar exception handlers but none of them included this level of detail. The contents of the web form has turned out to be quite valuable from time to time. I guess you could also include the contents of the entire request and response stream?
You can download a complete sample solution:
http://download.binaryocean.com/WebExceptionSolution.zip
Modify your Global.asax so that the Application_Error event calls the WebExceptionEmailer class and set your from and to addresses. The to address needs to point to you. For the from address, I generally use a smtp address that incorporates the name of the application or domain name of the web site. Make sure you have a local smtp server or change the code to correctly point to one.
void Application_Error(object sender, EventArgs e)
{
new WebExceptionEmailer().SendEmail("weberror@mywebapp.com", "myaddress@mycompany.com");
}
Here is the exception handling class:
using System;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web;
public class WebExceptionEmailer
{
public WebExceptionEmailer()
{
}
private StringBuilder errorText = new StringBuilder();
public void SendEmail(string fromAddress, string toAddress)
{
Exception ex = HttpContext.Current.Server.GetLastError().GetBaseException();
BuildExceptionDetail(ex);
BuildWebDetail();
MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress);
message.To.Add(toAddress);
message.Subject = ex.Message;
message.Body = errorText.ToString();
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "localhost";
smtpClient.Send(message);
}
private void BuildExceptionDetail(Exception ex)
{
errorText.Append("Message: " + ex.Message + Environment.NewLine + Environment.NewLine);
errorText.Append("Source: " + ex.Source + Environment.NewLine + Environment.NewLine);
errorText.Append("TargetSite: " + ex.TargetSite + Environment.NewLine + Environment.NewLine);
errorText.Append("StackTrace: " + ex.StackTrace + Environment.NewLine + Environment.NewLine);
// loop recursivly through the inner exception if there are any.
if (ex.InnerException != null)
{
errorText.Append("InnerException:" + Environment.NewLine + Environment.NewLine);
BuildExceptionDetail(ex.InnerException);
}
}
private void BuildWebDetail()
{
HttpContext context = HttpContext.Current;
errorText.Append("FullUrl: " + HttpContext.Current.Request.Url.OriginalString + Environment.NewLine + Environment.NewLine);
errorText.Append("Form Values:" + Environment.NewLine);
if (context.Request.Form != null)
{
foreach (string key in context.Request.Form.Keys)
{
errorText.Append(key + ": " + context.Request.Form[key].ToString() + Environment.NewLine);
}
}
errorText.Append(Environment.NewLine);
errorText.Append("Referer: ");
if (context.Request.ServerVariables["HTTP_REFERER"] != null)
{
errorText.Append(context.Request.ServerVariables["HTTP_REFERER"]);
}
errorText.Append(Environment.NewLine + Environment.NewLine);
errorText.Append("Host Address: " + context.Request.ServerVariables["LOCAL_ADDR"] + Environment.NewLine);
errorText.Append("Client Address: " + context.Request.UserHostAddress + Environment.NewLine + Environment.NewLine);
}
}