private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (!Configuration.Enabled || !AllowApplicationObject(sender as HttpApplication))
            {
                return;
            }

            HttpApplication app     = (HttpApplication)sender;
            HttpContext     context = app.Context;

            if (IsLRAP(context.Request))
            {
                return;
            }

            var page    = context?.CurrentHandler as Page;
            var handler = context?.CurrentHandler as IHttpHandler;

            if (page != null || handler != null)
            {
                //https://github.com/snives/HttpModuleRewrite
                bool requestCallbackExecuted = false;
                var  requestCallback         = new Func <string, string>(content =>
                {
                    requestCallbackExecuted = true;
                    var requestForm         = content != null ? HttpUtility.ParseQueryString(content) : null;

                    LoggingHelper.SetupSession(context, page, requestForm);
                    LoggingHelper.SetupPage(context, page, page != null ? LogType.OnPageRequest : LogType.OnHandlerRequestReceived, requestForm);

                    if (page != null)
                    {
                        LoggingPage.LogRequest(context, page, requestForm);
                        LoggingPage.LogSession(context, page, requestForm, before: true);
                    }
                    else if (handler != null)
                    {
                        LoggingHandler.LogRequest(context, requestForm);
                        LoggingHandler.LogSession(context, requestForm, before: true);
                    }

                    return(requestForm?.ToString());
                });
                this.context.Request.Filter = new RequestFilter(this.context.Request.Filter, context.Request.ContentEncoding, requestCallback);

                if (this.context.Request.Form == null || !requestCallbackExecuted) //Ensure requestCallback is being called to log session and request etc
                {
                    requestCallback(null);
                }
            }
        }
        private void Context_EndRequest(object sender, EventArgs e)
        {
            if (!Configuration.Enabled || !AllowApplicationObject(sender as HttpApplication))
            {
                return;
            }

            if (responseWatcher == null)
            {
                return; //e.g. Invalid web.config setting
            }
            string response = responseWatcher.ToString();


            HttpApplication application = (HttpApplication)sender;
            HttpContext     context     = application.Context;

            if (IsLRAP(context.Request))
            {
                LRAPHttpManager.ProcessLRAPFile(context);
                return;
            }

            var page    = context?.CurrentHandler as Page;
            var handler = context?.CurrentHandler as IHttpHandler;

            if (page != null || handler != null)
            {
                var sessionGUID = LoggingHelper.GetSessionGUID(context, page, () => new Guid());
                var pageGUID    = LoggingHelper.GetPageGUID(context, page, () => new Guid());
                var serverGUID  = LoggingHelper.GetServerGUID(context, () => null);

                if (page != null)
                {
                    response = LoggingPage.LogResponse(context, page, response);
                }
                else
                {
                    response = LoggingHandler.LogResponse(context, response);
                }

                if (context.Response.ContentType == ContentType.TextHtml)
                {
                    var lrapValues  = new LRAPValues(sessionGUID, pageGUID, serverGUID);
                    var newResponse = LRAPHttpManager.InsertLRAPScript(response, lrapValues);
                    context.Response.Clear();
                    context.Response.Write(newResponse);
                }
            }
        }