我正在寻找一种跟踪服务器生成页面所需时间的方法。我知道可以使用 Trace 来跟踪此项信息,但我需要一种按页面显示此信息的方法。
这是 ASP.Net MVC 2。
这是 ASP.Net MVC 2。
你可以像ActionFilterAttribute一样实现它。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class LoggingAttribute : ActionFilterAttribute
{
private readonly Stopwatch _sw;
public LoggingAttribute()
{
_sw = new Stopwatch();
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_sw.Start();
Debug.WriteLine("Beginning executing: " + GetControllerAndActionName(filterContext.ActionDescriptor));
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
_sw.Stop();
var ms = _sw.ElapsedMilliseconds;
Debug.WriteLine("Finishing executing: " + GetControllerAndActionName(filterContext.ActionDescriptor));
Debug.WriteLine("Time elapsed: "+ TimeSpan.FromMilliseconds(ms).TotalSeconds);
}
private string GetControllerAndActionName(ActionDescriptor actionDescriptor)
{
return actionDescriptor.ControllerDescriptor.ControllerName + " - " + actionDescriptor.ActionName;
}
}
使用装饰器将其应用到每个控制器或操作方法中,然后就可以轻松查看调试输出的文本。
编辑:如果你想在页面上打印它,你可以将这段代码添加到OnActionExecuted方法中。
if(filterContext.Result is ViewResult) { //Make sure the request is a ViewResult, ie. a page
((ViewResult) filterContext.Result).ViewData["ExecutionTime"] = ms; //Set the viewdata dictionary
}
现在你已经将执行时间保存在ViewData中,可以在页面中访问它。我通常会像这样将它放在主页面中:
<!-- The page took <%= ViewData["ExecutionTime"] %> ms to execute -->
是的,Derin建议是在ASP.NET应用程序中执行操作的标准方式,我建议添加一个if条件语句,以便它不会干扰非HTML响应:
编辑:添加完整实现
public class PerformanceMonitorModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += delegate(object sender, EventArgs e)
{
//Set Page Timer Star
HttpContext requestContext = ((HttpApplication)sender).Context;
Stopwatch timer = new Stopwatch();
requestContext.Items["Timer"] = timer;
timer.Start();
};
context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
{
HttpContext httpContext = ((HttpApplication)sender).Context;
HttpResponse response = httpContext.Response;
Stopwatch timer = (Stopwatch)httpContext.Items["Timer"];
timer.Stop();
// Don't interfere with non-HTML responses
if (response.ContentType == "text/html")
{
double seconds = (double)timer.ElapsedTicks / Stopwatch.Frequency;
string result_time = string.Format("{0:F4} sec ", seconds);
RenderQueriesToResponse(response,result_time);
}
};
}
void RenderQueriesToResponse(HttpResponse response, string result_time)
{
response.Write("<div style=\"margin: 5px; background-color: #FFFF00\"");
response.Write(string.Format("<b>Page Generated in "+ result_time));
response.Write("</div>");
}
public void Dispose() { /* Not needed */ }
}
您还可以为它添加一些样式...
并且请记得在httpModules部分的WebConfig中注册您的模块:
<add name="Name" type="namespace, dll"/>
要完整参考这个内容,请查看Steven Sanderson的《Pro ASP.NET MVC Framework》第15章——性能,监测页面生成时间。
编辑:(评论@Pino) 以下是我项目的示例: alt text http://www.diarioplus.com/files/pictures/example_performance.JPG
这将取决于您想在哪里包含此信息。例如,您可以编写一个http处理程序,在</html>
标记后显示呈现时间:
public class RenderTimeModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, e) =>
{
var watch = new Stopwatch();
var app = (HttpApplication)sender;
app.Context.Items["Stopwatch"] = watch;
watch.Start();
};
context.EndRequest += (sender, e) =>
{
var app = (HttpApplication)sender;
var watch = (Stopwatch)app.Context.Items["Stopwatch"];
watch.Stop();
var ts = watch.Elapsed;
string elapsedTime = String.Format("{0} ms", ts.TotalMilliseconds);
app.Context.Response.Write(elapsedTime);
};
}
public void Dispose()
{
}
}
如果你想在html页面的中间显示渲染时间,那么这个渲染时间不会计算总页面渲染时间。
Application_Start
处理程序中将属性添加到GlobalFiltersCollection中,以避免在每个类/操作上都进行装饰。 - Justin Rusbatch