Finding the execution time of a action in mvc 3 using global action filters

Posted by Unknown
Finding the execution time of a action in MVC 3 using global action filters.

Today i found a very interesting issue while working with global action filters in MVC,we can measure the execution time of the  action with the help of "OnResultExecuting" and "OnResultExecuted" method in the action filter attribute.

Here we can track the user activities and logs globally in global action filter attribute.So each and every request can be captured in action filter attribute.

Here is the step by step explanation of how to implement global action filter with example of measuring excution time of a action.

public class StopwatchAttribute : ActionFilterAttribute
{
    private readonly Stopwatch _stopwatch;
 
    public StopwatchAttribute()
    {
        _stopwatch = new Stopwatch();
    }
 
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _stopwatch.Start();
    }
 
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _stopwatch.Stop();
 
        var httpContext = filterContext.HttpContext;
        var response = httpContext.Response;
        var elapsed = _stopwatch.Elapsed.ToString();
 
        // Works for Cassini and IIS
        //response.Write(string.Format("<!-- X-Stopwatch: {0} -->", elapsed));  
 
        // Works for IIS
        response.AddHeader("X-Stopwatch", elapsed);
    }
}  



Now register these filter in gloabal.asax aplication_Start

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
 
    GlobalFilters.Filters.Add(new StopwatchAttribute());
 
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}


Now create the controller with a sample action 


public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewModel.Message = "Welcome to ASP.NET MVC!";
 
        return View();
    }
 
    public ActionResult About()
    {
        return View();
    }
}



Now run your mvc application, when action executing it will starts the timer on 
"OnActionExecuting" and stops the timmer in "OnActionExecuted".and displays the 
execution time in responce header or if you can write it in responce.write like 
below.

Output of StopwatchAttribute



Here if you want too track or log the user activities you can use



public class UserActivityAttribute : ActionFilterAttribute
{
  public override void OnResultExecuting(ResultExecutingContext filterContext)
  {
    if ((filterContext.Result is RedirectToRouteResult) &&
        (filterContext.RequestContext.HttpContext.Request.RequestType == "POST"))
    {
      var originController = filterContext.RouteData.Values["controller"].ToString();
      var originAction = filterContext.RouteData.Values["action"].ToString();
 
      if (originAction == "Create")
      {
 
      }
    }
    base.OnResultExecuting(filterContext);
  }
}
Hope this will helpl you
Labels: ,

Post a Comment

 
test