【问题标题】:Using MVC Miniprofiler for every action call对每个动作调用使用 MVC Miniprofiler
【发布时间】:2011-06-28 13:55:08
【问题描述】:

我一直在尝试伟大的工具,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 语句一起使用,我该怎么做。

【问题讨论】:

  • 如果您下载 Mini Profiler 的源代码,那么示例项目中有一个基本控制器类就可以做到这一点。

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


【解决方案1】:

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

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

差不多就这些了。

【讨论】:

  • 达林 MVC 2 有没有等效的方法?
  • 哈实际上我只需要将 [ProfileActions] 添加到我的 BaseController,这很容易。
  • 如果您想知道执行了哪个 Controller 和 Action 然后使用 var step = profiler.Step("Controller: " + filterContext.RouteData.Values["controller"].ToString() + " Action: " + filterContext.ActionDescriptor.ActionName); ,希望对某人有所帮助。
【解决方案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();
        }
    }
}

【讨论】:

  • 我投了赞成票,因为 Application_AcquireRequestState 最终成为我检查用户是否已通过身份验证并有权使用/查看分析器的好地方,但这并不能严格解决 OP 的问题。这次是请求,而不是具体的操作。次要细节,但可能很重要。
  • 有很多工具可以跟踪请求处理时间。但是这种方法特别适用于跟踪经过身份验证的请求的处理时间。它讲述了每个用户及其等待时间的故事,而不仅仅是一般的请求。
猜你喜欢
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
相关资源
最近更新 更多