Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which ASP.NET life cycle events fire after HttpApplication.Error?

I want to know which parts of the ASP.NET request life cycle happen after an error is handled via the HttpApplication.Error event. Specifically, which of the events listed at http://msdn.microsoft.com/en-us/library/bb470252.aspx#Stages fire after an error? I know that EndRequest still fires, and I suspect that PreSendRequestHeaders and PreSendRequestContent fire as well, but aside from these I have no idea.

Does it depend on when in the life cycle the error occurs? Does it depend on whether I call Server.ClearError() in the error handler?

I'm asking this question because I don't know if I should be calling HttpApplication.CompleteRequest() from my error handler.

like image 298
William Gross Avatar asked Nov 02 '10 15:11

William Gross


People also ask

Which event is fired after it has been initialised?

Init. This event fires after each control has been initialized. Each control's UniqueID is set and any skin settings have been applied. Use this event to read or initialize control properties.

What is the last event of web page life cycle?

UnLoad - The UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed.

How many stages are there in ASP.NET web page life cycle?

In ASP.NET, a web page has execution lifecycle that includes various phases. These phases include initialization, instantiation, restoring and maintaining state etc. it is required to understand the page lifecycle so that we can put custom code at any stage to perform our business logic.

Which event of Page cycle is the viewstate available?

The Viewstate is actually loaded in the OnPreLoad event of the page,Just after the Page_InitComplete.


1 Answers

The best way is to catch server last error and appdomain exceptions.

All of them can be done in Global.asax.cs file.

Check the following steps:

1- In Global.asax.cs, catch the last error and log it.

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Server.ClearError();
        log.Error("Application Error caught in Global ", exception);
    }

2- Add an event handler for UnhandledException event on AppDomain, this should be added to the Application_Start :

    protected void Application_Start(object sender, EventArgs e)
    {
        //....
        AppDomain.CurrentDomain.UnhandledException 
              += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    }       

3- And here is the implementation of CurrentDomain_UnhandledException:

    void CurrentDomain_UnhandledException(object sender, 
                                                   UnhandledExceptionEventArgs e)
    {
      if (e != null)
        log.Error("Domain Unhandled Exception: ", e.ExceptionObject as Exception);
    }

Happy coding :)

like image 116
Houman Avatar answered Oct 24 '22 19:10

Houman