15
A
回答
14
没错的Derin建议是标准的方式做到这一点在ASP.NEt应用程序,我只想建议增加一个,如果因此它不会与非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"/>
对于一个完整的参考这个检查Pro ASP.NET MVC框架作者:Steven Sanderson - 第15章 - 性能,监控页面生成时间。
编辑:我在说 alt text http://www.diarioplus.com/files/pictures/example_performance.JPG
谢谢 - 效果很好,除了这似乎输出后,打破我的网页:(建议? – LiamB 2009-12-01 15:46:17
元素