Reputation: 75
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.
Upvotes: 3
Views: 1424
Reputation: 48583
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?
Upvotes: 3
Reputation: 16435
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).
Upvotes: 2
Reputation: 43067
Upvotes: 3
Reputation: 18181
I would do both:
Upvotes: 0
Reputation: 162
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.
Upvotes: 1