我正在为一个内部项目使用优秀的MVC Mini Profiler,但无论你是谁,都想让它显示时间信息。理想情况下,我希望能够显示完整的剖析信息,如果用户是管理员或网站的开发人员,并只显示整体时间信息,如果用户只是一个标准用户…
MVC mini profiler是可行的吗,还是我应该在站点上添加秒表?我们正在使用Solr作为我们的后端,所以我想说"Solr在x毫秒内得到了结果,我们在y毫秒内渲染了页面“,目前我们可以(在一定程度上)做到这一点,但仅适用于开发人员……我们是否可以从分析器中获得这些数字,然后自己显示它们,或者我是不是走错了路?
发布于 2011-11-11 01:24:37
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
)
);
}
}
}https://stackoverflow.com/questions/8078751
复制相似问题