19Aug ASP.NET 计算每个页面的加载速度 Aug 19, 2020 By 余钊 1266 Views 0 Comments public class HttpModule : IHttpModule { public void Dispose() { } 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, seconds, context.Request.RawUrl); } }; } void RenderQueriesToResponse(HttpResponse response, double result_time,string page) { //response.Write("<div style=\"margin: 5px; background-color: #FFFF00\""); //response.Write(string.Format("<b>Page Generated in " + result_time)); //response.Write("</div>"); SqlDB db = new SqlDB(); string[] paraName = new string[2] { "@page", "@result_time" }; string[] paraValue = new string[2] { page, result_time.ToString() }; db.ExecuteNonQueryByProc("AddPageLoadSpeedData", paraName, paraValue); } } 转载请注明出处: http://bluesd7.com/蓝影闪电的随笔/ContentId/136/ASP-NET-计算每个页面的加载速度 标签: ASP.NET, httpmodule, Ihttpmodule 分类: .Net, C#, 后端开发, 性能优化, 原创