asp.net计算每个页面执行时间的方法

2025-05-29 0 90

本文实例讲述了asp.net计算每个页面执行时间的方法。分享给大家供大家参考。具体分析如下:

这里的asp.net代码可实现计算每个页面的执行时间,无需要修改页面的相关代码,这段代码会给所有的页面统一加上执行时间显示

  1. publicclassPerformanceMonitorModule:IHttpModule
  2. {
  3. publicvoidInit(HttpApplicationcontext)
  4. {
  5. context.PreRequestHandlerExecute+=delegate(objectsender,EventArgse)
  6. {
  7. //SetPageTimerStar
  8. HttpContextrequestContext=((HttpApplication)sender).Context;
  9. Stopwatchtimer=newStopwatch();
  10. requestContext.Items["Timer"]=timer;
  11. timer.Start();
  12. };
  13. context.PostRequestHandlerExecute+=delegate(objectsender,EventArgse)
  14. {
  15. HttpContexthttpContext=((HttpApplication)sender).Context;
  16. HttpResponseresponse=httpContext.Response;
  17. Stopwatchtimer=(Stopwatch)httpContext.Items["Timer"];
  18. timer.Stop();
  19. //Don'tinterferewithnon-HTMLresponses
  20. if(response.ContentType=="text/html")
  21. {
  22. doubleseconds=(double)timer.ElapsedTicks/Stopwatch.Frequency;
  23. stringresult_time=string.Format("{0:F4}sec",seconds);
  24. RenderQueriesToResponse(response,result_time);
  25. }
  26. };
  27. }
  28. voidRenderQueriesToResponse(HttpResponseresponse,stringresult_time)
  29. {
  30. response.Write("<divstyle=\\"margin:5px;background-color:#FFFF00\\"");
  31. response.Write(string.Format("<b>PageGeneratedin"+result_time));
  32. response.Write("</div>");
  33. }
  34. publicvoidDispose(){/*Notneeded*/}
  35. }

希望本文所述对大家的asp.net程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 asp.net计算每个页面执行时间的方法 https://www.kuaiidc.com/100870.html

相关文章

发表评论
暂无评论