The ActionFilterAttribute is a great way to create a global logging procedure for entry and exit of an ActionResult method in your MVC Controllers.
Create a class that inherits from ActionFilterAttribute and implements IActionFilter.
[AttributeUsage(AttributeTargets.Method)]
public sealed class LogRequestAttribute : ActionFilterAttribute, IActionFilter
{
///
/// Log exit of the ActionResult method
///
///
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
// Put code you want to log when the ActionResult has completed in here.
}
///
/// Log entry into the ActionResult method
///
///
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
// Put code you want to log when the ActionResult is entered in here.
}
}
To implement in your code, all you do is add the attribute to the ActionResult method:
[LogRequestAttribute]
public ActionResult ChangePasswordSuccess()
{
return View();
}
When the ActionResult executes, it will hit in this order:
- OnActionExecuting
- OnActionExecuted
Using the ActionFilterAttribute saves you from having to write the same code over and over for every method in your Controller.
Here’s an example of using the ActionFilterAttribute for rolling your own security from Adrew Siemer’s blog:
#1 by Ogi Ivanov on October 1, 2009 - 9:14 am
Quote
This is a very nice article and an awesome blog. I wish you all the best with it.