使用MVC Miniprofiler进行每个动作调用

时间:2011-06-28 13:55:08

标签: asp.net-mvc mvc-mini-profiler

我一直在试验这个伟大的工具,Mvc MiniProfiler

我不想使用大量Step命令丢弃所有视图,所以我想在每个动作调用中使用探查器。馊主意?这是我到目前为止所尝试的:

 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);
            }
        }
}

但我认为这不是我想做的事情吗?我想我需要在OnActionExecuting上启动探查器并在OnResultExecuted上停止它。考虑到探查器旨在与using语句一起使用,我该如何做到这一点。

2 个答案:

答案 0 :(得分:24)

您可以定义全局操作过滤器:

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();
        }
    }
}

并在Global.asax注册:

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

这就是全部。

答案 1 :(得分:2)

考虑并使用ActionFilter是可以的。但是MVC仍然是一个ASP .NET应用程序。要在每个请求的开始和结束时启动和停止MiniProfiler,您可以在Global.asax.cs文件中尝试应用程序事件。

// --------------------------------------------------------------------------------------------------------------------
// <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();
        }
    }
}