开发者

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 fr开发者_StackOverflow中文版om 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.


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 :)


The LogRequest event is raised even if an error occurs. You can provide an event handler for the LogRequest event to provide custom logging for the request. for further details on http://msdn.microsoft.com/en-us/library/system.web.httpapplication.logrequest.aspx

If your application generates custom error output, suppress the default error message that is generated by ASP.NET by a call to the ClearError method in the HttpApplication.Error Event.


I didn't check, but I think it depends.

An error can be raised in any page event (Init/Load/PreRender). After the error is raised, if you clear the error, the page lifecycle is continued from where it was.


The exception that raises the Error event can be accessed by a call to GetLastError method. If your application generates custom error output, suppress the default error message generated by ASP.NET by a call to the ClearError method.


void Application_Error(Object sender, EventArgs e)

in global.asax

http://msdn.microsoft.com/en-us/library/24395wz3%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/fwzzh56s%28v=vs.100%29.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜