ASP.NET Global.asax error logger

You probably know that you should never show ASP.NET error message to the client. Method below should be placed in Global.asax, so it would send error notification to the developer in case of any error. Email is going to have details of exception, URL of the page, referer page, IP address of the client, Form and Session values, user name of logged user if logged in.  Response would be transfered to general error page, which not gonna have any details of actual exception. If error page has an error ( it happens if masterpage has error) then methods simply prints error message using Response.Write.

 void Application_Error(object sender, EventArgs e)


    // Code that runs when an unhandled error occurs

    string testEnvironment = ConfigurationSettings.AppSettings["isTestEnvironment"];

    if (testEnvironment == "0")


        Exception ex = Server.GetLastError();

         if (ex is HttpException && ex.InnerException is ViewStateException)


        StringBuilder theBody = new StringBuilder();

        theBody.Append("URL: " + Request.Url + "\n");

        theBody.Append("Referer: " + Request.ServerVariables["HTTP_REFERER"] + "\n");

        theBody.Append("IP: " + Request.ServerVariables["REMOTE_HOST"] + "\n");

        theBody.Append("Error Message: " + ex.ToString() + "\n");

        if (User.Identity.IsAuthenticated)

            theBody.Append("User: " + User.Identity.Name + "\n");


            theBody.Append("User is not logged in." + "\n");

        theBody.Append("Form Values: " + "\n");

        foreach (string s in Request.Form.AllKeys)


            if (s != "__VIEWSTATE")

                theBody.Append(s + ":" + Request.Form[s] + "\n");


        theBody.Append("Session Values: " + "\n");

        foreach (string s in Session.Keys)

            theBody.Append(s + ":" + Session[s] + "\n");

        System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();

        email.IsBodyHtml = false;

        email.From = new System.Net.Mail.MailAddress("", "ErrorManager");

        email.To.Add(new System.Net.Mail.MailAddress("", "Developer"));

        email.Subject = Request.Url.ToString().Split('/')[2] + " has ASP.NET error";

        email.Body = theBody.ToString();



            System.Net.Mail.SmtpClient emailProvider = new System.Net.Mail.SmtpClient();



        catch (Exception anException)





            if (Request.Url.Segments[Request.Url.Segments.Length - 1].ToLower() != "error.aspx")




                Response.Write(@"We encountered an internal error. We apologize for any inconvenience

                but know that our staff gets emailed EVERY error that occurs so that we can solve it promptly.");







Posted on Wednesday, March 19, 2008 by | Comments (4) | Add Comment



Re:ASP.NET Global.asax error logger

this is nice artical......This is helpfull for me..

Posted on 5/20/2011 12:53:37 AM by Puneet Malviya #

Re:ASP.NET Global.asax error logger

Yeaaaaa !!!.. for me too.
Thanks a lot

Posted on 8/17/2011 11:39:41 AM by market #

Re: ASP.NET Global.asax error logger

I try this code and it gives me error.
'Session state is not available in this context.'
at 'foreach (string s in Session.Keys)'
what should i do?

Posted on 1/31/2012 2:33:15 AM by khaing #

You can wrap foreach statement with if statement:
if (HttpContext.Current.Session!=null) { foreach (string s in Session.Keys) ... }.


Re: ASP.NET Global.asax error logger

thnx, nice example...

Posted on 10/17/2012 10:31:54 PM by narendra #

New Comment

Your Name:
Email (for internal use only):
Code above:


Valid HTML5