private void LogEvent(object sender, EventArgs e, string name, StreamWatcher watcher)
        {
            try
            {
                if (((HttpApplication)sender).Context.Request.CurrentExecutionFilePathExtension.ToLower() == ".aspx")
                {
                    HttpApplication app           = (HttpApplication)sender;
                    HttpContext     context       = app.Context;
                    string          filePath      = context.Request.FilePath;
                    string          fileExtension = VirtualPathUtility.GetExtension(filePath);
                    var             page          = (Page)context.CurrentHandler;
                    Log($"HttpModuleTest_{name}", context, page, null, watcher);
                }

                if (((HttpApplication)sender).Context.Request.CurrentExecutionFilePathExtension.ToLower() == ".ashx")
                {
                    HttpApplication app           = (HttpApplication)sender;
                    HttpContext     context       = app.Context;
                    string          filePath      = context.Request.FilePath;
                    string          fileExtension = VirtualPathUtility.GetExtension(filePath);
                    var             handler       = (IHttpHandler)context.CurrentHandler;
                    Log($"HttpModuleTest_Handler_{name}", context, null, handler, watcher);
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void Context_BeginRequest(object sender, EventArgs e)
        {
            if (((HttpApplication)sender).Context.Request.CurrentExecutionFilePathExtension.ToLower() == ".axd")
            {
                return;
            }

            watcher = new StreamWatcher(this.context.Response.Filter);
            this.context.Response.Filter = watcher;

            LogEvent(sender, e, "Context_BeginRequest", watcher);

            HttpApplication app           = (HttpApplication)sender;
            HttpContext     context       = app.Context;
            string          filePath      = context.Request.FilePath;
            string          fileExtension = VirtualPathUtility.GetExtension(filePath);

            //if (filePath.ToLower() == "/logrecorderandplayerhandler.lrap")
            //{
            //    LoggingHelper.LogHandlerRequest(context.Request["request"]);
            //    return;
            //}

            //try
            //{
            //    if (app.Session != null)
            //    {
            //        app.Session["WHATTHEFUCK"] = "WOOHOOO";
            //    }
            //}
            //catch (System.Exception)
            //{
            //}

            //if (context.Session != null)
            //{
            //    context.Session["WHATTHEFUCK"] = "WOOHOOO";
            //}
        }
        private string BuildLogText(string name, HttpContext context, Page page, IHttpHandler handler, StreamWatcher watcher)
        {
            var sb = new StringBuilder();

            try
            {
                sb.AppendLine($"context: {context != null}");
                if (context != null)
                {
                    sb.AppendLine($"context.Session: {context.Session != null}");
                    if (context.Session != null)
                    {
                        sb.AppendLine($"context.Session[\"HttpModuleTest\"]: {(context.Session["HttpModuleTest"] ?? "null")}");
                        var value = $"{name}{DateTime.Now.ToString("HH:mm:ss:fff")}";
                        sb.AppendLine($"Setting context.Session[\"{value}\"] = \"{value}\"");
                        context.Session[value] = value;
                        sb.AppendLine($"Done setting context.Session[\"{value}\"] = \"{value}\"");
                    }
                }
            }
            catch (Exception ex)
            {
                sb.AppendLine($"context: Error");
                sb.AppendLine(ex.Message);
                sb.AppendLine(ex.StackTrace);
            }
            sb.AppendLine($"page: {page != null}");
            try
            {
                if (page != null && page.IsValid)
                {
                    try
                    {
                        sb.AppendLine($"page.Session: {page.Session != null}");
                        if (page.Session != null)
                        {
                            sb.AppendLine($"page.Session[\"HttpModuleTest\"]: {(page.Session["HttpModuleTest"] ?? "null")}");
                            var value = $"{name}{DateTime.Now.ToString("HH:mm:ss:fff")}";
                            sb.AppendLine($"Setting page.Session[\"{value}\"] = \"{value}\"");
                            page.Session[value] = value;
                            sb.AppendLine($"Done setting page.Session[\"{value}\"] = \"{value}\"");
                        }
                    }
                    catch (Exception ex)
                    {
                        sb.AppendLine($"Page-Session: Error");
                        sb.AppendLine(ex.Message);
                        sb.AppendLine(ex.StackTrace);
                    }
                }
            }
            catch (Exception ex2)
            {
                sb.AppendLine("Page: Error");
                sb.AppendLine(ex2.Message);
                sb.AppendLine(ex2.StackTrace);
            }

            if (page != null)
            {
                SetupPageEvent(page, "InitComplete");
                SetupPageEvent(page, "LoadComplete");
                SetupPageEvent(page, "PreInit");
                SetupPageEvent(page, "PreLoad");
                SetupPageEvent(page, "PreRenderComplete");
                SetupPageEvent(page, "SaveStateComplete");
                SetupPageEvent(page, "DataBinding");
                SetupPageEvent(page, "Disposed");
                SetupPageEvent(page, "Init");
                SetupPageEvent(page, "Load");
                SetupPageEvent(page, "PreRender");
                SetupPageEvent(page, "Unload");
                SetupPageEvent(page, "AbortTransaction");
                SetupPageEvent(page, "CommitTransaction");
                SetupPageEvent(page, "Error");
            }
            if (page != null)
            {
                sb.Append(TestViewState(page, name));
            }

            sb.AppendLine($"handler: {handler != null}");

            if (watcher != null)
            {
                try
                {
                    sb.AppendLine($"watcher.response.length = {watcher.ToString().Trim().Length}");
                }
                catch (Exception ex)
                {
                    sb.AppendLine($"watcher: Error");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                }
            }

            return(sb.ToString());
        }
        private void Log(string name, HttpContext context, Page page, IHttpHandler handler, StreamWatcher watcher)
        {
            var f = System.IO.File.CreateText($"c:\\HttpModuleTest\\{DateTime.Now.ToString("HHmmssfff")}_{name}.txt");

            f.Write(BuildLogText(name, context, page, handler, watcher));
            f.Close();
        }