mvc-mini-profiler - 让 mvc-mini-profiler 向所有用户显示一些数据

标签 mvc-mini-profiler

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

MVC 迷你分析器是可行的方法,还是我应该在网站上添加秒表?我们使用 Solr 作为后端,所以我希望能够说“Solr 在 x 毫秒内得到结果,我们在 y 毫秒内渲染页面”,我们目前可以(在一定程度上)做到这一点,但仅限于开发人员。 ..我们可以从分析器中获取这些数字,然后自己显示它们,还是我在这里走错了树?

最佳答案

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-mini-profiler - 让 mvc-mini-profiler 向所有用户显示一些数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8078751/

相关文章:

c# - 来自 Stack Overflow 的 MVC 迷你分析器(安装)

c# - MiniProfiler 没有出现在 asp.net MVC 上

asp.net-membership - MiniProfiler 和 SqlMembershipProvider

c# - 没有嵌套使用的步进 MVC Mini Profiler

asp.net-mvc - donut 孔缓存 - 排除 MiniProfiler.RenderIncludes

servicestack - 如何获取仅用于服务的 Mini-Profiler 网页

c# - mvc mini profiler 中 HttpRuntime 缓存存储的限制

asp.net-mvc - 如何从重定向的请求中获取 MiniProfiler 日志?

c# - MiniProfiler 在 ASP.NET MVC2 中工作吗?

ruby-on-rails - Rails 局部渲染有 10 倍的峰值渲染时间