Gathering useful information about an error in ASP.NET?
<p>I am examining our legacy system, and there is a method that sends an e-mail when there is an exception.</p>

<p>It looks like this:</p>

<pre><code>  string strError = "Error in: " + Request.Path + 
   "\nUrl: " + Request.RawUrl + "\n\n";

  // Get the exception object for the last error message that occured.
  Exception ErrorInfo = Server.GetLastError().GetBaseException();
  strError += "Error Message: " + ErrorInfo.Message + 
   "\nError Source: " + ErrorInfo.Source + 
   "\nError Target Site: " + ErrorInfo.TargetSite + 
   "\n\nQueryString Data:\n-----------------\n";

  // Gathering QueryString information
  for (int i = 0; i &lt; Context.Request.QueryString.Count; i++) 
   strError += Context.Request.QueryString.Keys[i] + ":\t\t" + Context.Request.QueryString[i] + "\n";
  strError += "\nPost Data:\n----------\n";

  // Gathering Post Data information
  for (int i = 0; i &lt; Context.Request.Form.Count; i++) 
   strError += Context.Request.Form.Keys[i] + ":\t\t" + Context.Request.Form[i] + "\n";
  strError += "\n";

  if (User.Identity.IsAuthenticated) strError += "User:\t\t" + User.Identity.Name + "\n\n";

  strError += "Exception Stack Trace:\n----------------------\n" + Server.GetLastError().StackTrace + 
   "\n\nServer Variables:\n-----------------\n";

  // Gathering Server Variables information
  for (int i = 0; i &lt; Context.Request.ServerVariables.Count; i++) 
   strError += Context.Request.ServerVariables.Keys[i] + ":\t\t" + Context.Request.ServerVariables[i] + "\n";
  strError += "\n";

  // Sending error message to administration via e-mail
    SmtpClient smtp = new SmtpClient();
    MailMessage email = new MailMessage();
    email.From = new MailAddress("noreply@mysite.com");
    email.To.Add("me@mysite.com");
    email.Subject = "Site error notification";
    email.Body = strError;

    smtp.Send(email);
</code></pre>

<p>This may or may not be hideous, some of the code in our system is beyond shocking, but I have never attempted to investigate the best way to report errors. What do you think?</p>
