首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让mvc-mini-profiler向所有用户显示一些数据

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

Stack Overflow用户
提问于 2011-11-10 19:24:02
回答 1查看 325关注 0票数 2

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

MVC mini profiler是可行的吗,还是我应该在站点上添加秒表?我们正在使用Solr作为我们的后端,所以我想说"Solr在x毫秒内得到了结果,我们在y毫秒内渲染了页面“,目前我们可以(在一定程度上)做到这一点,但仅适用于开发人员……我们是否可以从分析器中获得这些数字,然后自己显示它们,或者我是不是走错了路?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-11 01:24:37

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

我不能接受过滤器的功劳,因为我在博客上找到了几乎相同的东西(但不记得在哪里)。

代码语言:javascript
复制
/// <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
                )
              );
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8078751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档