Centre for Software Practice

the blog
Welcome to Centre for Software Practice Sign in | Join | Help
in Search

Tim's blog

Good Error Reporting for ASP.Net Websites with MS-AJAX

A requirement in many ASP.Net web applications out there is that you the developer know when your website breaks, the usual way was to use the Application_Error event and then send your self an email or write to a log file.  With the event of AJAX itergration into ASP.Net v2.0 the Application_Error event does not run when the request was an Asyncronous PostBack, fear not this is a fixable problem.  This article is will cover my personal opinion on a good practice method of error sending. Firstly i will breifly go over the method to run the Application_Error event.

First Step go to the root of the website and add a new item, add the Global Application Class File leave the file name alone then click Add. you should now have a file Called Global.asax in the root of your website. you can make the Global.asax use a code behind file, which i would recomend.  The code for error handling looks something like this:

Global.asax:

<%@ Application Inherits="CSP.Global" Language="C#" %>

App_Code/Global.asax.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

namespace CSP
{
    /// <summary>
    /// Summary description for Global
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
        void Application_Error(object sender, EventArgs e)
        {
          // Code that runs when an unhandled error occurs
          ExceptionHandling.SendException(Server.GetLastError(), HttpContext.Current);
        }

    }
}

This Code runs a function that sends an error message email to the developers in charge of the application, where the first argument is of type Exception and the second of type HttpContext.  The HttpContext.Current object can give us information about the request that caused the error, the session state when it happened, the user who caused the error (via Session state, cookie or IP address, etc) and more.

On to AJAX exception handling. To my surprise the when an error occured during an AJAX postback the above code never got run so i had to track down what the AJAX alternative was.  It was quite simple to find it turned out, the error event seems to be handled by the ScriptManger Control which oddly enough has a OnAsyncPostBackError Event.  The point of this post was not my "grand" discovery but rather the best practice method, so back to that.  The best way to handle this event was to make sure it was centrally handled once a site was setup and didn't need to be worried about on any subsequent new pages therefore it was perfect to put this into the Master Page.

 In the master page put a scriptmanager in the document body inside the server side form tags like so:

        <ajax:ScriptManager ID="ScriptManagerMain" runat="server" EnablePartialRendering="true"
            OnAsyncPostBackError="ScriptManagerMain_AsyncPostBackError">
        </ajax:ScriptManager>

The code to run the error should look something like this:

    protected void ScriptManagerMain_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
    {
        CSP.ExceptionHandling.SendException(e.Exception, HttpContext.Current);
    }
 

Note the event arguments for this event are custom and you can get the Exception Error from them.

If your website has a combination of these 2 error catching methods you can rest assured that your application is working smoothly whilst running AJAX and that all errors are being reported.

Published Friday, 20 July 2007 9:37 AM by TimHenstridge

Comments

No Comments
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems