It should be a straight forward and simple answer, but I'm trying to figure out where to put the logging into my MVC application. Is it wise to put it in the Controller or the Model?
A:
Your logging should be in the Controller since that is where the processing of logic should take place.
The Model is for storing data.
Justin Niessner
2010-06-25 14:11:33
Nargh. Keep controllers slim and models fat. http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model
David Dorward
2010-06-25 14:40:03
@David Dorward: this is what I thought.
Ardman
2010-06-25 14:42:35
@David Dorward - That article doesn't apply here. Ruby on Rails uses the Active Record pattern for data access and the article shows moving data access logic to a method on the Model. In .NET MVC we don't use Active Record, we use Repositories and View Models. That article pretty much says "move retrieval logic to the Repository and View related Model data to a ViewModel" which makes perfect sense. It doesn't, however, make sense for logging.
Justin Niessner
2010-06-25 14:46:14
+2
A:
Try this SO question:
http://stackoverflow.com/questions/569252/logging-errors-in-asp-net-mvc
Hope that helps
Ryano
2010-06-25 14:36:20
+1
A:
Use custom action filters to handle logging.
take a look at the following article for info >> http://www.asp.net/mvc/tutorials/understanding-action-filters-cs
Mark
2010-06-25 14:37:01
And even better, take advantage of the MVC extensibility points to automatically include your exception filters everywhere. (No more annoying attributes!) This is much cleaner than a base controller in my opinion. http://geekswithblogs.net/wesm/archive/2009/12/11/ijoined-filter.aspx
Ryan
2010-06-25 14:43:54