Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling and logging strategy in .NET

I am building a multi-layered application that has an ASP.NET MVC web application. It conists of the usuals like presentation layer, business layer, data layer, etc. How would one create/use a decent exception handling mechanism? I read on Patterns and Practices that you need to bubble up exceptions to the various layers.

Also logging. Where does logging take place? In the MVC web application?

How would you redirect to various error pages depending on the type of error?

I would appreciate some feedback regarding this, and some articles if you guys have some. If there are any sample apps that make use of a decent exception hadling and logging strategy please let me know :)

like image 644
Brendan Vogt Avatar asked Mar 04 '11 11:03

Brendan Vogt


People also ask

What is exception handling and logging?

Logging and exception handling are like two peas in a pod. When a problem happens in your Java code, that typically means you have an exception that needs to be handled, and of course, any time an error or unanticipated event occurs, that occurrence should be logged appropriately.

What is exception handling in .NET framework?

Exception handling is a mechanism in which a programming construct is used to consistently trap, intercept and handle the error occurred during application execution.

What is exception handling strategy?

An exception handling strategy consists of all actions and conventions necessary to properly handle exceptions in your application. The following diagram shows an overview of the various parts of an exception handling strategy, from the error is detected, all the way up to where the error is handled.


1 Answers

First, I would suggest reading the article "Vexing Exceptions" by Eric Lippert. This should give you some reasonable guidance on exception-handling (and more on exception throwing).

When it comes to exception logging, the easiest and cleanest approach is to have a "top-level" exception handler responsible for dealing with all otherwise unhandled exceptions and record them to a log for analysis. This can be done in ASP.NET applications through the HttpApplication.Error event which you can hook into through your Global.asax file.

If a part of the application is trapping an error (catching it) and handling it in some way, it may be appropriate to log a warning or informational at that point, so can record a problem occurred, but that the system dealt with it. Try to avoid filling your code with these though, as they can quickly become a maintenance problem.

like image 162
Paul Turner Avatar answered Oct 02 '22 00:10

Paul Turner