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

发布于 2024-12-14 10:20:16 字数 290 浏览 3 评论 0原文

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

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

I am using the excellent MVC Mini Profiler for an internal project, but would like to get it to show timing information, no matter who you are. ideally, i would like to be able to show the full profiling information if the user is an admin or developer of the site, and show just overall timing info if the user is just a standard user...

would MVC mini profiler be the way to go, or should i just add stopwatches to the site? We are using Solr for our backend, so i would like to be able to say "Solr got results in x miliseconds and we rendered the page in y miliseconds" which we can do (to an extent) at the moment but only for developers... can we get those numbers from the profiler, and then display them ourselves, or am i embarking up the wrong tree here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2024-12-21 10:20:16

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

MiniProfiler is probably fine or you could register a global filter similar to the code below. Obviously style appropriately for your scenario (see the response.Write(...) portion near the bottom).

I can't take credit for the filter because I found something nearly identical on a blog (don't remember where, though).

/// <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
                )
              );
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文