页面生成时间 - ASP.Net MVC [英] Page generation time - ASP.Net MVC
问题描述
我正在寻找一种方法来跟踪它多长时间要对由服务器生成的页面。我知道我可以使用跟踪来跟踪这一点,但我需要一种方法来每页显示此。
I'm looking for a way to track how long it took for a page to be generated by the server. I know I can use Trace to track this but I need a way to display this per page.
它的ASP.Net MVC 2
Its ASP.Net MVC 2
推荐答案
没错的Derin建议是标准的方式做到这一点在ASP.NEt应用程序,我只想建议增加一个,如果因此它不会与非干扰HTML响应:
编辑:添加完整的实施
Yep the Derin Suggestion is the standard Way to do it in an ASP.NEt application, i would just suggest add an if so it does not interfere with non-HTML responses: added complete implementation
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 */ }
}
您还可以添加一些样式,...
you can also add some style to it...
和记注册的HttpModules部分在里面你WebConfig模块:
And remember to register your Module in WebConfig inside httpModules Section:
<add name="Name" type="namespace, dll"/>
有关这个完整的参考检查临ASP.NET MVC框架由史蒂芬·桑德森 - 第15章 - 性能,监视页面生成时间
For a Complete Reference about this check the Pro ASP.NET MVC Framework by Steven Sanderson - Chapter 15 - Performance, Monitoring Page Generation Times.
编辑:(评论@Pino)
这里是我的项目的例子:
(comment @Pino) Here is the example for my project:
这篇关于页面生成时间 - ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!