gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:08:22 24 4
gpt4 key购买 nike

我正在为一个内部项目使用出色的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com