3

I have an ASP.NET MVC application that is supposed to catch all unhandled exceptions within the global.asax application error handler.

If I define the handler as follows:

protected void Application_Error(object sender, EventArgs e)

then it works fine. However, if within the Application_Start event I try and do:

this.Error += new EventHandler(Application_Error);

The actual event is never called.

Does anyone know why and if so what i'm doing incorrectly?

2 Answers 2

3

You shouldn't have to add to the error event explicitly; Application_Error should get called automatically by the framework.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll up vote this for the first comment, but I don't believe the second part is true, I'm currently running my application with out the CustomErrors element in my webconfig and it's hitting the Error handler fine.
Thanks, not sure where I go that from . Edited my response.
0

The event is not being called either because Exceptions are being caught somewhere else in the application (and swallowed maybe) or because you are registering the event in the Application_Start event (not needed). You want to do something like this in your Application_Error:

protected void Application_Error(object sender, EventArgs e) {
  Exception exception = Server.GetLastError();
  // log exception
  // Email exception
}

I use this method to both log and E-mail uncaught errors for all my applications. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.