views:

295

answers:

5

We are building an ASP.NET application in C#

We have 3 layers: UI, Business and Database. We need clarification on error handling/logging. We have a error logging framework to log errors.

My question is: do we need to log errors in each layer or only in the main calling layer (UI layer) by throwing the errors to UI layer from business and database layers? Is there a best practice?

If would be great if you could also provide a reference document or web references (If needed).

Thanks and Regards … Sruthi Keerthi.

+3  A: 
jrummell
@Jeff - you're right. I modified it to better answer the question.
jrummell
+1 from me as a consequence (and for the good 4guys link, since it sounds like the original poster wants some non-SO confirmation) - sorry to delete the initial context for that
Jeff Sternal
I disagree. If you have a choice of handling exception at the source or at a centralized location such as the HTTpApplication.Error event, I would handle the exception at the centralized location. Otherwise exception logging code (if this all you are doing) will be throughout your codebase.In the case where resources need to be released then of course you must handle the exception at the source.
Chuck Conway
I just read the article, it has sound advice. The wording of the first bullet point is strange. It makes me think I should all the exceptions at the point of throw and then have code to catch the ones I couldn't handle.
Chuck Conway
+1, especially for ELMAH
Stefan
@Charles, I think we are the same page.By 'handle' I did mean 'do something about'. That something would be whatever makes sense in the given context, other than logging or swallowing (catching and doing nothing). I agree that logging should be done a centralized location in the UI layer. Elmah is great for this because it automagically logs all unhandled exceptions with an IHttpModule.
jrummell
+1  A: 

We recently started working on some ASP.net applications. For the logging of errors we use log4net (based on log4j). With this dll you can call different kind of logging methods, like a fatal error or just an info message.

I suggest you should not throw errors from different layers. If an error occurs on that layer it should stay there and be logged on the appropriate location. Else this is a lot of work and it will ruin the maintainability of your application.

J. Visser
A: 

I would do both:

  1. Log error where it happens, which is a MUST.
  2. Display to UI, depending on your choice, you can either display the full error message plus stack trace, or just display a customized friendly error message which provides enough information to look up the error you logged in step 1.
J.W.
How does this work? How is it logged where it happened?
Chuck Conway
In my applications, I do whatever I can to log the error "near" the code that generate it. But I make sure I've a catch all in my application (Elmah or by putting some code in Application_Error) to handle and log all unhandled errors. I never display the real message to the user. It tells nothing to the end user and it gives possible informations to hacker about your code. If I can give a usefull message for the user, I do. Else, I give a generic error page (ex : Something wrong happened. Please retry).
mberube.Net
+2  A: 

If you can do something about an Exception in a given infrastructure layer, catch it there, handle it, then log it with your logging framework of choice if it's still appropriate to do so. For example, I've seen SqlExceptions in data access layers that represented deadlocks. Of course it would be better to eliminate the deadlocks, but until we could diagnose them it made sense to catch those Exceptions and retry a certain number of times.

If you cannot handle it at that level - if there's nothing sensible to do with it - don't catch it there. Just let it bubble up to the highest level context where you can do something about it, or if there's nothing you can do about it, let it bubble all the way to your executable / UI where you can log it and display something appropriate to the user (that is, not the stack trace or Exception message).

For additional details (and excellent advice) see this similar question, Should I catch exceptions only to log them?

Jeff Sternal
This. Most certainly this. +1 for mentioning that expected exceptions should be handled, unhandleable ones thrown up the chain.
Chad Ruppert
+1  A: 

We always let the errors bubble to the top. Then we attach to the HttpApplication.Error event. At that point we decide what to do (i.e. log to the DB, file, show friendly message... etc).

Chuck Conway