Mike Weerasinghe has made a good question regarding my previous post about error handling. Mike says that he”s handling errors by using a module which handles the HttpApplication error event. Unfortunately, this will stop working when you use AJAX extensions because the ScriptManager control will handle the error and automatically return a response to the client stopping the event from being propagated to the HttpApplication object. So, if you”re handling the Error event of that class, you won”t be notified about the error and your logging will stop working.
The solution to this is to handle the Error of the Page since the methods that handle it will always be called if you get an exception during a partial postback. Since I think nobody will want to add their logging logic to all the pages and will probably want to keep handling the error from a module, then there”s a workaround which might just work with you. What you need is to handle the PostMapRequestHandler event, which is fired by the HttpApplication object after mapping the current request to a handler. During that event, you can check for the current handler and see if it”s a page. If it is, then you just need to handle the Error event and you should be ready to go. Here”s a sample which shows what you need in order for it to work (I”m pasting the code from global.asax, but it should also work from a module though i haven”t tested that):
protected void Application_PostMapRequestHandler(object sender, EventArgs e)
Page aux = this.Context.Handler as Page;
if( aux != null )
aux.Error += new EventHandler(page_Error);
void page_Error(object sender, EventArgs e)
//log error as you want
By hooking the error event like this, your method will always be called before the one introduced by ScriptManager and you will have access to the current exception. Just don”t clear it because if you do,the ScriptManager”s method will not get the info it need to its work.
Hope this helps…
Update: I was so eager to get a general error handling solution that i”ve just forgot about object lifetime! As my good friend João Paulo said in the comments,my previous solution might be bad if you don”t want to leak resources (and yes, I do believe that you don”t). As he said, I should remove my event handler so that I don”t keep the page alive. However, there”s a problem with the PostRequestHandlerExecute event handling: though I could be mistaken, but I think it won”t be called due to the way that ScriptManager handles the Error event generated by the page: it calls Response.End after packaging the response which is being sent back to the client. So, removing the method from the event list during the page_Error method might be the best option to this scenario (specially if you know that the page has a ScriptManager control)
btw, thanks for the correction João…and don”t be a stranger! 🙂
Update2: re-reading my previous update made me think that I didn”t explain myself correctly. My previous remarks are only valid for when an exception happens in a page that has a ScriptManager control. If there isn”t an exception, you still should hook a post execute handler event in order to unsubscribe the event handling…