我基于两篇文章创建了一个自定义模块:
http://community.devexpress.com/blogs/paulk/archive/2009/03/30/implementing-an-ihttpmodule.aspx
该模块基于:
How do I display the duration it took to generate a page in a pages footer?
我添加了以下课程:
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 */ }
} // End of PerformanceMonitorModule
以下是我的web.config:
<system.web>
...
<httpModules>
<add name="PerformanceMonitorModule" type="PerformanceMonitorModule"/>
</httpModules>
</system.web>
我在PerformanceMonitorModule
类init方法中设置了断点,它永远不会被调用。
一些注意事项:
我不知道还有什么可以尝试才能加载这个模块?