Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MVC Miniprofiler for every action call

Iv been experimenting the great tool, Mvc MiniProfiler.

I don't want to litter all my view with lots of Step commands, so I am wanting to use the profiler with every action call. Bad idea? This is what I have tried so far:

 public abstract class BaseController : Controller
 {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var profiler = MiniProfiler.Current;
            using (profiler.Step("Action: "+filterContext.ActionDescriptor.ActionName))
            {
                base.OnActionExecuting(filterContext);
            }
        }
}

But I don't think this is doing what I am intending? I think I need to start the profiler on OnActionExecuting and stop it on OnResultExecuted. How do I do this, considering the profiler is designed to be used with the using statement.

like image 360
Dan Avatar asked Jun 28 '11 13:06

Dan


2 Answers

You could define a global action filter:

public class ProfileActionsAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var profiler = MiniProfiler.Current;
        var step = profiler.Step("Action: " + filterContext.ActionDescriptor.ActionName);
        filterContext.HttpContext.Items["step"] = step;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var step = filterContext.HttpContext.Items["step"] as IDisposable;
        if (step != null)
        {
            step.Dispose();
        }
    }
}

and register in Global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new ProfileActionsAttribute());
}

and that's pretty much all.

like image 175
Darin Dimitrov Avatar answered Nov 03 '22 09:11

Darin Dimitrov


It is alright to think and use ActionFilter. But MVC is still an ASP .NET application. To start and stop MiniProfiler at beginning and ending of each request, you can try the application events in Global.asax.cs file.

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="Believe2014">
//   http://believeblog.azurewebsites.net/post/miniprofiler--log4net
// </copyright>
// <summary>
//   The mvc application.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Mvc4Application
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    /// <summary>
    ///     The mvc application.
    /// </summary>
    public class MvcApplication : HttpApplication
    {
        /// <summary>
        ///     The application_ start.
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            Profiler.Initialize();
        }

        /// <summary>
        /// The event when the application acquires request state.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument..
        /// </param>
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            Profiler.Start(HttpContext.Current);
        }

        /// <summary>
        /// This function is called by ASP .NET at the end of every http request.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument.
        /// </param>
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            Profiler.Stop();
        }
    }
}
like image 27
Believe2014 Avatar answered Nov 03 '22 09:11

Believe2014