Logging of errors and tracing bugs is a very important part of development, so we’ve put together a script which records all relevant information at the point of the error, and logs it to our Error Tracking System, as well as sends out an email to the site administrator with the error.
First, we want to record the website address where this error occurred, so we get this in 2 parts from
ErrorPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.Path;
RawUrl = Request.RawUrl;
Next, we get the last exception from the server stack
Exception ErrorInfo = Server.GetLastError().GetBaseException(); if (ErrorInfo != null) { strError += "Error Message: " + ErrorInfo.Message + "nError Source: " + ErrorInfo.Source + "nError Target Site: " + ErrorInfo.TargetSite; }
The Error Type and Source
ErrorType = Server.GetLastError().GetType().ToString();
if (Server.GetLastError().GetType() == typeof(HttpUnhandledException)) { ErrorSource = (Server.GetLastError() as HttpUnhandledException).GetHtmlErrorMessage(); }
The QueryString data, Post data and Session variables
for (int i = 0; i < Context.Request.QueryString.Count; i++) { QueryStringData += Context.Request.QueryString.Keys[i] + ":tt" + Context.Request.QueryString[i] + "n"; }
for (int i = 0; i < Context.Request.Form.Count; i++) { PostData += Context.Request.Form.Keys[i] + ":tt" + Context.Request.Form[i] + "n"; }
for (int i = 0; i < Context.Session.Count; i++) { SessionData += Context.Session.Keys[i] + ":tt" + Context.Session[i] + "n"; }
If the user is authenticated, we can get his username
if (User.Identity.IsAuthenticated) { UserAccountName = User.Identity.Name; }
Next, we record the Stack Trace
StackTrace = Server.GetLastError().StackTrace;
Last, the Server Variables
for (int i = 0; i < Context.Request.ServerVariables.Count; i++) { ServerVariables += Context.Request.ServerVariables.Keys[i] + ":tt" + Context.Request.ServerVariables[i] + "n"; }
And now we have all the information we need to later retrace and duplicate the error to allow us to fix it.
Our Error Tracking System, once released will allow you to group the errors by Url and Error Type, and view all this information in a very helpful manner.
Finally, we send an email to the developer with all the information we’ve gathered
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(); m.To.Add(System.Configuration.ConfigurationManager.AppSettings["DebugEmail"]); m.Subject = "Error occurred on " + Request.ServerVariables["HTTP_HOST"]; m.IsBodyHtml = false; m.Body = strError; System.Net.Mail.SmtpClient c = new System.Net.Mail.SmtpClient(); try { c.Send(m); } catch { }
To make our life easier when deploying this script to many websites, we store a few variables in web.config under appSettings
And finally, we set our mail server
Complete code to be placed in your global.asax
void Application_Error(object sender, EventArgs e) { ErrorTracking.TrackingInfo info = new ErrorTracking.TrackingInfo(); info.ErrorPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.Path; info.RawUrl = Request.RawUrl; string strError = "Error in: http://" + Request.ServerVariables["HTTP_HOST"] + Request.Path + "nUrl: " + Request.RawUrl + "nn"; try { // Get the exception object for the last error message that occured. Exception ErrorInfo = Server.GetLastError().GetBaseException(); if (ErrorInfo != null) { strError += "Error Message: " + ErrorInfo.Message + "nError Source: " + ErrorInfo.Source + "nError Target Site: " + ErrorInfo.TargetSite; info.ErrorMessage = ErrorInfo.Message; } strError += "Error Type: " + Server.GetLastError().GetType().ToString(); info.ErrorType = Server.GetLastError().GetType().ToString(); if (Server.GetLastError().GetType() == typeof(HttpUnhandledException)) { strError += "Source: " + (Server.GetLastError() as HttpUnhandledException).GetHtmlErrorMessage(); info.ErrorSource = (Server.GetLastError() as HttpUnhandledException).GetHtmlErrorMessage(); } info.QueryStringData = ""; strError += "nnQueryString Data:n-----------------n"; // Gathering QueryString information for (int i = 0; i < Context.Request.QueryString.Count; i++) { strError += Context.Request.QueryString.Keys[i] + ":tt" + Context.Request.QueryString[i] + "n"; info.QueryStringData += Context.Request.QueryString.Keys[i] + ":tt" + Context.Request.QueryString[i] + "n"; } strError += "nPost Data:n----------n"; info.PostData = ""; // Gathering Post Data information for (int i = 0; i < Context.Request.Form.Count; i++) { strError += Context.Request.Form.Keys[i] + ":tt" + Context.Request.Form[i] + "n"; info.PostData += Context.Request.Form.Keys[i] + ":tt" + Context.Request.Form[i] + "n"; } strError += "nSession Data:n----------n"; // Gathering Session information for (int i = 0; i < Context.Session.Count; i++) { strError += Context.Session.Keys[i] + ":tt" + Context.Session[i] + "n"; } strError += "n"; if (User.Identity.IsAuthenticated) { strError += "User:tt" + User.Identity.Name + "nn"; info.UserAccountName = User.Identity.Name; } info.StackTrace = Server.GetLastError().StackTrace; strError += "Exception Stack Trace:n----------------------n" + Server.GetLastError().StackTrace + "nnServer Variables:n-----------------n"; // Gathering Server Variables information info.ServerVariables = ""; for (int i = 0; i < Context.Request.ServerVariables.Count; i++) { strError += Context.Request.ServerVariables.Keys[i] + ":tt" + Context.Request.ServerVariables[i] + "n"; info.ServerVariables += Context.Request.ServerVariables.Keys[i] + ":tt" + Context.Request.ServerVariables[i] + "n"; } strError += "n"; } catch (Exception s) { strError += s.Message; } info.CompleteSource = strError; info.SiteKey = Guid.Parse(System.Configuration.ConfigurationManager.AppSettings["ErrorTrackingKey"]); info.IsRead = false; info.IsResolved = false; info.DateLogged = DateTime.Now; info.ErrorCause = ErrorTracking.ErrorCauses.Unknown; info.UserAccountID = Guid.Empty; info.ErrorSource = ""; info.UserAccountName = ""; ErrorTracking.ErrorTrackingServiceClient client = new ErrorTracking.ErrorTrackingServiceClient(); client.SubmitError(info); // Send email to developer System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(); m.To.Add(System.Configuration.ConfigurationManager.AppSettings["DebugEmail"]); m.Subject = "Error occurred on " + Request.ServerVariables["HTTP_HOST"]; m.IsBodyHtml = false; m.Body = strError; System.Net.Mail.SmtpClient c = new System.Net.Mail.SmtpClient(); try { c.Send(m); } catch { } }
Comments are off this post