14

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 :)

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234

5 Answers5

10

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.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
6

In my MVC3 application I use ELMAH for the exception handling as described here How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?, while if I need to log a custom message I use the Trace methods that comes from the System.Diagnostics, here a useful link Overriding System.Diagnostics.Trace.WriteLine to log to a file.

UPDATE Now you can set up elmah in your application directly with the NuGet packager. http://gregorsuttie.wordpress.com/2011/02/02/elmah-using-nuget-what-they-are-and-why-you-should-use-them-part-1/

Community
  • 1
  • 1
stuzzo
  • 1,056
  • 1
  • 15
  • 36
  • I am looking for a exception handling and logging solution that can be used application wide. So a down click for you :) – Brendan Vogt Mar 04 '11 at 11:47
  • @Brendan Vogt: And why you can't use in this way?I use the solution proposed widely.On the other hand,I use also the bubbling of the exceptions.If I got an exception from db methods, I catch that and report a new Exception like BusinessLayerException.Sorry if I've misunderstanded your question.I love share ideas :D! – stuzzo Mar 04 '11 at 12:14
2

Take a look at Enterprise Library. It will provide you with a very flexible logging tool and other goods I think you'll enjoy. For example: you can use the Police Injection Application Block (taht implements AOP) to catch all exceptions in your code without writing a line of code.

GRGodoi
  • 1,946
  • 2
  • 24
  • 38
1

Most .NET logging frameworks have some built-in functionality to support ASP.NET (or should); for Serilog it is the Serilog.Extras.Web package on NuGet. There's not much to configure, installing the package into your Serilog app will cause ASP.NET errors to be logged &c.

(I work on Serilog and just wrote a post about pretty much this scenario.)

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
1

I would use Log4Net wrapped in a custom logger business class so from everywhere in the code nobody knows we are using Log4net and it would be easy to change the logging framework in the future if needed. For exception handling, as usual if you don't handle it throw it up logging anyway method name, actual parameters values and so on. There were many discussions here in SO about when to catch, when to hide and when to throw exceptions...

Of course the main thread ( User Interface ) should have an unhandled exception event handler which will log everything properly and not just crash the application.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147