views:

329

answers:

1

We have a fist MVC application and I am wandering what special considerations there. In the regular web forms applications we pretty much just wrap events on code behind pages with try catch blocks. But it seems less clear with mvc as far as helpers, controllers, routes, etc. Where should I make sure there is logging.

+2  A: 

Typically you would apply logging in your controller actions. You can also derive from the HandleError attribute to add error logging, as well as create custom action/result filters to automate certain types of logging. With such a filter, you can apply this to your base controller and have some basic usage logging/instrumentation for all actions.

If you customize the framework with controller factories, action invokers, or model binders, you might apply logging there as well. Of course, this is nothing to say of how to apply logging to your actual Model (domain objects, external services, persistence and database, security etc).

gWiz
We usually (in WebForm apps) also do logging in the Application_Error event in Global.asax. Does this also apply in MVC or is there a more "suitable" place to handle all unhandled exceptions?
Peter Lillevold
Application_Error is perfectly appropriate for across-the-board exception logging. In fact I would prefer that to subclassing HandleError, because it would also log exceptions in code running in threads not directly handling to web requests (such as code in Application_Start).
gWiz