Ejemplo n.º 1
0
        private void BeginRequest(object sender, EventArgs e)
        {
            HttpContext ctx     = HttpContext.Current;
            var         request = ctx.Request;

            KissLog.Web.WebProperties webProperties = new KissLog.Web.WebProperties
            {
                Request = HttpRequestFactory.Create(request)
            };

            ctx.Items[Constants.HttpRequestPropertiesKey] = webProperties;

            Logger logger = Logger.Factory.Get() as Logger;

            if (logger == null)
            {
                return;
            }

            logger.DataContainer.WebProperties = webProperties;

            KissLog.Internal.NotifyListeners.NotifyBeginRequest(webProperties.Request, logger);
        }
Ejemplo n.º 2
0
        internal void OnEndRequest(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (httpContext.Response == null)
            {
                InternalLogger.LogException(new NullHttpResponseException(nameof(OnEndRequest)));
                return;
            }

            if (httpContext.Request == null)
            {
                InternalLogger.LogException(new NullHttpRequestException(nameof(OnEndRequest)));
                return;
            }

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(httpContext);

            // IIS redirect bypasses the IHttpModule.BeginRequest event
            if (logger.DataContainer.HttpProperties == null)
            {
                KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(httpContext.Request);
                logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));
            }

            MirrorStreamDecorator responseStream = GetResponseStream(httpContext.Response);
            long contentLength = responseStream == null ? 0 : responseStream.MirrorStream.Length;

            KissLog.Http.HttpResponse httpResponse = HttpResponseFactory.Create(httpContext.Response, contentLength);
            logger.DataContainer.HttpProperties.SetResponse(httpResponse);

            Exception ex = httpContext.Server?.GetLastError();

            if (ex != null)
            {
                logger.Error(ex);
            }

            if (responseStream != null)
            {
                if (KissLog.InternalHelpers.CanReadResponseBody(httpResponse.Properties.Headers))
                {
                    if (ShouldLogResponseBody(logger, factory, httpContext))
                    {
                        ILogResponseBodyStrategy logResponseBody = LogResponseBodyStrategyFactory.Create(responseStream.MirrorStream, responseStream.Encoding, logger);
                        logResponseBody.Execute();
                    }
                }

                responseStream.MirrorStream.Dispose();
            }

            IEnumerable <Logger> loggers = factory.GetAll(httpContext);

            KissLog.InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyListeners.NotifyFlush.Notify(loggers.ToArray());
            });
        }
Ejemplo n.º 3
0
        private void EndRequest(object sender, EventArgs e)
        {
            HttpContext ctx = HttpContext.Current;

            Logger logger = Logger.Factory.Get() as Logger;

            if (logger == null)
            {
                return;
            }

            Exception ex = ctx.Server.GetLastError();

            if (ex != null)
            {
                logger.Log(LogLevel.Error, ex);
            }

            var sniffer = ctx.Response.Filter as ResponseSniffer;

            if (ctx.Response.StatusCode >= 400 && ex == null)
            {
                if (sniffer != null)
                {
                    string responseContent = null;

                    try
                    {
                        responseContent = sniffer.GetContent();
                    }
                    catch (Exception ex1)
                    {
                        InternalHelpers.Log(ex1.ToString(), LogLevel.Error);
                    }

                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        logger.Log(LogLevel.Error, responseContent);
                    }
                }
            }

            WebProperties webProperties = (WebProperties)HttpContext.Current.Items[Constants.HttpRequestPropertiesKey];

            if (webProperties == null)
            {
                // IIS redirect bypasses the BeginRequest() event
                webProperties = new WebProperties
                {
                    Request = HttpRequestFactory.Create(ctx.Request)
                };
            }

            KissLog.Web.HttpResponse response = HttpResponseFactory.Create(ctx.Response);
            webProperties.Response = response;

            if (logger.DataContainer.ExplicitHttpStatusCode.HasValue)
            {
                response.HttpStatusCode = logger.DataContainer.ExplicitHttpStatusCode.Value;
            }

            if (sniffer != null)
            {
                response.Properties.ContentLength = ReadResponseLength(sniffer);

                LogResponse(logger, response, sniffer);
            }

            logger.DataContainer.WebProperties = webProperties;

            IEnumerable <ILogger> loggers = Logger.Factory.GetAll();

            Logger.NotifyListeners(loggers.ToArray());
        }