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
元素