Reputation: 95
I've got an MVC application where I use Try-Catch blocks throughout and I'm looking for an easy way to implement activity/error-logging based on a global boolean field. Does anyone know a way I can 'extend' the functionality of the Try-Catch code in order to add my own activity/error logging without having to go through and touch every block?
For example:
I'd like the the Try to automatically log certain items to my DB such as url, action, ids, parameters, IP info, etc,... And the Catch to automatically log it's Exception to another table in the DB.
Thoughts? Recommendations?
Thanks, Peter
Upvotes: 3
Views: 1184
Reputation: 11088
Create your custom action filter and decorate controllers or action methods with it.
http://msdn.microsoft.com/en-us/library/dd381609.aspx
public class LoggingFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " +
filterContext.ActionDescriptor.ActionName);
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Exception != null)
filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown");
base.OnActionExecuted(filterContext);
}
}
Then use it
public class HomeController : Controller
{
[LoggingFilter]
public ActionResult Index()
{//...}
}
Upvotes: 5
Reputation: 88385
I wouldn't necessarily suggest using this, but the Microsoft Enterprise Library has a "block" (i.e. module) that helps with managing exceptions at different levels. You could take a look at what they do for ideas, or even use it if it looks like it would meet your needs:
http://msdn.microsoft.com/en-us/library/ff664698(v=PandP.50).aspx
Upvotes: 0
Reputation: 47789
The best way to do this is to funnel every single error through a global handler method ... not the application_error handler mind you, because that is only invoked for unhandled errors, but in the catch blocks. Although this will require you to touch every catch block, you only have to do it once, and from then on you can add global functionality like the one you describe in only one place.
The way I like to think about it is by thinking about the Single Responsibility Principle. Things like handling errors should be handled (for the most part) somewhere else other than the class in which the error is caught.
Upvotes: 1
Reputation: 218960
Logging is a very commonly cited cross-cutting concern for what you're talking about, which is called Aspect Oriented Programming. There's a lot of information about it, but be aware that it's often not for the faint of heart in .NET developers. It's kind of a strange thing to wrap your head around (and I won't pretend that I've successfully done that).
Upvotes: 0
Reputation: 67273
I know of no way to extend the existing try/catch block. However, you can implement a global event handler in Global.asax for all exceptions that are unhandled.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
Beyond that, you may need to add a class that you call from all your exception handlers.
Maybe create an extension method on Exception
so that your global method can be very easily called and will have access to all the exception details.
Upvotes: 0