The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PerformanceMonitorModule.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Teknik.Utilities;
  10. namespace Teknik.Modules
  11. {
  12. public class PerformanceMonitorModule : IHttpModule
  13. {
  14. public void Dispose() { /* Nothing to do */ }
  15. public void Init(HttpApplication context)
  16. {
  17. context.PreRequestHandlerExecute += delegate (object sender, EventArgs e)
  18. {
  19. HttpContext requestContext = ((HttpApplication)sender).Context;
  20. Stopwatch timer = new Stopwatch();
  21. requestContext.Items["Timer"] = timer;
  22. timer.Start();
  23. };
  24. context.PostRequestHandlerExecute += delegate (object sender, EventArgs e)
  25. {
  26. HttpContext requestContext = ((HttpApplication)sender).Context;
  27. Stopwatch timer = (Stopwatch)requestContext.Items["Timer"];
  28. timer.Stop();
  29. // Don't interfere with non-HTML responses
  30. if (requestContext.Response.ContentType == "text/html" && requestContext.Response.StatusCode == 200 && !new HttpRequestWrapper(requestContext.Request).IsAjaxRequest())
  31. {
  32. double ms = (double)timer.ElapsedMilliseconds;
  33. string result = string.Format("{0:F0}", ms);
  34. requestContext.Response.Write(
  35. "<script nonce=\"" + requestContext.Items[Constants.NONCE_KEY] + "\">" +
  36. "var pageGenerationTime = '" + result + "';" +
  37. "pageloadStopTimer();" +
  38. "</script >");
  39. }
  40. };
  41. }
  42. }
  43. }