Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BaseModule : IHttpModule
- {
- private const double Treshold = 5;
- private string _filePath;
- public void Init(HttpApplication context)
- {
- _filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data");
- context.PreRequestHandlerExecute += PreRequestHandler;
- context.PostRequestHandlerExecute += PostRequestHandler;
- }
- public void Dispose()
- {
- }
- protected void PreRequestHandler(object sender, EventArgs e)
- {
- var requestContext = ((HttpApplication)sender).Context;
- var timer = new Stopwatch();
- // Store timer in request
- requestContext.Items["Timer"] = timer;
- timer.Start();
- }
- protected void PostRequestHandler(object sender, EventArgs e)
- {
- var requestContext = ((HttpApplication)sender).Context;
- var timer = requestContext.Items["Timer"] as Stopwatch;
- if (timer == null)
- return;
- timer.Stop();
- var elapsedSeconds = timer.Elapsed.Seconds;
- if(elapsedSeconds > Treshold)
- {
- // Log slow running page
- File.AppendAllText(Path.Combine(_filePath, "slow-loading.txt"), GetWarning(requestContext, elapsedSeconds));
- }
- }
- protected string GetWarning(HttpContext context, double elapsedSeconds)
- {
- var sb = new StringBuilder();
- sb.AppendLine(string.Format("[{0}] [{1} seconds] ({2} {3}) ", DateTime.Now, elapsedSeconds, context.Request.HttpMethod, context.Request.Url));
- if(context.Request.HttpMethod == "POST")
- {
- for(var i = 0; i < context.Request.Form.Count; i++)
- {
- sb.AppendLine(string.Format("{0}: {1}", context.Request.Form.GetKey(i), context.Request.Form[i]));
- }
- }
- sb.AppendLine("-------------");
- return sb.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement