我根据两篇文章创建了一个自定义模块:
http://community.devexpress.com/blogs/paulk/archive/2009/03/30/implementing-an-ihttpmodule.aspx
该模块基于:
我添加了以下类:
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 方法中设置了断点,它永远不会被调用。
几点注意事项:
- 该网站在 HTTPS 上运行。我不认为这会有所作为吗?
- 该网站在 Azure 计算机模拟器中运行。不确定这是否会导致任何问题。
- 我尝试将程序集限定名称(MySite.Website.Business.Modules.PerformanceMonitorModule、MySite.Website、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null)。
我不确定还有什么方法可以让这个模块加载?