【问题标题】:getting mvc-mini-profiler to show some data to all users让 mvc-mini-profiler 向所有用户显示一些数据
【发布时间】:2011-11-11 01:24:36
【问题描述】:

我正在为一个内部项目使用出色的 MVC Mini Profiler,但无论你是谁,我都希望它能够显示时间信息。理想情况下,如果用户是网站的管理员或开发人员,我希望能够显示完整的分析信息,如果用户只是标准用户,则只显示总体时间信息......

MVC mini profiler 是可行的方法,还是我应该只在网站上添加秒表?我们在后端使用 Solr,所以我想说“Solr 在 x 毫秒内得到结果,我们在 y 毫秒内呈现页面”,我们目前可以(在一定程度上)这样做,但仅限于开发人员。 .. 我们可以从分析器中获取这些数字,然后自己显示它们,还是我在这里启动了错误的树?

【问题讨论】:

    标签: mvc-mini-profiler


    【解决方案1】:

    MiniProfiler 可能没问题,或者您可以注册一个类似于以下代码的全局过滤器。显然适合您的场景的样式(请参阅底部附近的 response.Write(...) 部分)。

    我不能把过滤器归功于我,因为我在博客上发现了几乎相同的东西(但不记得在哪里)。

    /// <summary>
    /// Filter to display the execution time of both the action and result
    /// </summary>
    public class RequestTimingFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Returns a Stopwatch instance for the specific context to gather
        /// diagnostics timing for
        /// </summary>
        /// <param name="context"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static Stopwatch GetTimer(ControllerContext context, string name)
        {
            var key = string.Format("__timer__{0}", name);
            if (context.HttpContext.Items.Contains(key))
            {
                return (Stopwatch)context.HttpContext.Items[key];
            }
    
            var result = new Stopwatch();
            context.HttpContext.Items[key] = result;
            return result;
        }
    
        /// <summary>
        /// Called before an action method executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            GetTimer(filterContext, "action").Start();
        }
    
        /// <summary>
        /// Called after the action method executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            GetTimer(filterContext, "action").Stop();
        }
    
        /// <summary>
        /// Called before an action result executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            GetTimer(filterContext, "render").Start();
        }
    
        /// <summary>
        /// Called after an action result executes.
        /// </summary>
        /// <param name = "filterContext">The filter context.</param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            var renderTimer = GetTimer(filterContext, "render");
            renderTimer.Stop();
    
            var actionTimer = GetTimer(filterContext, "action");
            var response = filterContext.HttpContext.Response;
    
            if (response.ContentType == "text/html")
            {
                response.Write(
                  string.Format(
                    "<div style='font-size: 70%; font-weight: bold; color: #888;'>Action '{0} :: {1}'<br /> Execute: {2}ms, Render: {3}ms.</div>",
                    filterContext.RouteData.Values["controller"],
                    filterContext.RouteData.Values["action"],
                    actionTimer.ElapsedMilliseconds,
                    renderTimer.ElapsedMilliseconds
                    )
                  );
            }
        }
    }
    

    【讨论】:

    • 感谢您的代码。非常便利。将考虑实施这个......在我开始使用它之前,它会干扰迷你分析器吗?谢谢。
    • 没有。它不应该影响迷你分析器。它只是一个 MVC ActionFilter,它捕获动作/结果的开始/结束时间并在您的页面中显示增量。它可能会显示在 mini-profiler 的执行配置文件中,但仅此而已。
    • actionfilter 好运吗?
    • ActionFilter 代码在顶部工作得很好,只是在尝试将其输出到正确的位置时遇到了问题(在当前形式的关闭 HTML 标记之后输出......需要调整它)。但它按计划工作。
    • 是的。我从来没有考虑过它,但可能值得将硬编码的样式信息从输出标记中取出,并只给 div 标签一个 id。那么你可能会使用 CSS 来移动它或使用一些 jQuery 来切换显示?只是一个想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多