views:

127

answers:

2

I work on a rather large web site. We currently have 4 web servers and an active passive db cluster running asp.net 2.0 with C#. Currently our exception handling is not trapping the correct exception being thrown. I have heard it is because server.getlasterror() is not thread safe (note: we currently do not use server.getlasterror().getbaseexception which, in my opinion, is why we are not getting the correct error). We are currently starting a project to trap exact exceptions so we can actually see the root cause of the error and I’m trying to find the best way to do it.

Our options are:

  • Go through all of our classes/methods (hundreds, if not thousands) to add try/catch/finally blocks to trap the correct exception.
  • Figure out a way to properly handle the exceptions in the global.asax.

So I guess my questions are:
Is server.getlasterror() thread safe?
If two exceptions are thrown at the same time, will both be logged?
Is there a better way to handle this than what I have listed?

Thanks for any help you can provide

+1  A: 

You should take a look at Steven A. Lowe's CALM product.

It's a really nice 'plug-in' solution.

(Disclaimer: I was one of the beta-testers)

Andrew Rollings
+1 for being so helpful!
Steven A. Lowe
+1  A: 

I believe that you should not alter the initial application at all. You should create and register at web.config an IHttpModule that hooks on Error event to log recursively the exception thrown and it's inner exceptions.

Edit:

Here are some references

http://msdn.microsoft.com/en-us/library/aa479332.aspx http://dotnetslackers.com/articles/aspnet/ErrorLoggingModulesAndHandlers.aspx http://www.codeproject.com/KB/aspnet/ELMAHDemo.aspx

Diadistis
How is this different than the application_error method in Global.asax?
Well it isn't but consider the following points http://fuchangmiao.blogspot.com/2008/03/globalasax-vs-httpmodule.html before making a decision.
Diadistis
And this : http://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx
Diadistis
this solution is good unless you want to redirect, which modules do not handle well; there is a work-around, though it's convoluted; see http://blogs.microsoft.co.il/blogs/oshvartz/archive/2008/05/17/asp-net-error-handling-using-httpmodule-full-and-partial-post-back-ajax-updatepanel.aspx
Steven A. Lowe
+1 for ELMAH, looks like just what I'm looking for.
Wilka