Exemple #1
0
 private void TestResponseProperties(KissLog.Http.HttpResponse httpResponse, KissLog.RestClient.Requests.CreateRequestLog.Http.ResponseProperties result)
 {
     Assert.AreEqual(httpResponse.StatusCode, result.HttpStatusCode);
     Assert.AreEqual(((HttpStatusCode)httpResponse.StatusCode).ToString(), result.HttpStatusCodeText);
     Assert.AreEqual(JsonSerializer.Serialize(httpResponse.Properties.Headers), JsonSerializer.Serialize(result.Headers));
     Assert.AreEqual(httpResponse.Properties.ContentLength, result.ContentLength);
 }
Exemple #2
0
        public void CreateResponsePropertiesCopiesAllProperties()
        {
            KissLog.Http.HttpResponse httpResponse = CommonTestHelpers.Factory.CreateHttpResponse();
            var result = PayloadFactory.Create(httpResponse);

            TestResponseProperties(httpResponse, result);
        }
        internal static ResponseProperties Create(KissLog.Http.HttpResponse httpResponse)
        {
            if (httpResponse == null)
            {
                throw new ArgumentNullException(nameof(httpResponse));
            }

            return(new ResponseProperties
            {
                HttpStatusCode = httpResponse.StatusCode,
                HttpStatusCodeText = ((HttpStatusCode)httpResponse.StatusCode).ToString(),
                Headers = httpResponse.Properties.Headers.ToList(),
                ContentLength = httpResponse.Properties.ContentLength,
            });
        }
Exemple #4
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());
            });
        }
Exemple #5
0
        public async Task Invoke(HttpContext context)
        {
            KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(context.Request);

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

            logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));

            KissLog.InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyListeners.NotifyBeginRequest.Notify(httpRequest);
            });

            ExceptionDispatchInfo ex = null;

            if (context.Response.Body != null && context.Response.Body is MirrorStreamDecorator == false)
            {
                context.Response.Body = new MirrorStreamDecorator(context.Response.Body);
            }

            try
            {
                await _next(context);
            }
            catch (Exception e)
            {
                ex = ExceptionDispatchInfo.Capture(e);
                throw;
            }
            finally
            {
                MirrorStreamDecorator responseStream = GetResponseStream(context.Response);
                long contentLength = responseStream == null ? 0 : responseStream.MirrorStream.Length;
                int  statusCode    = context.Response.StatusCode;

                if (ex != null)
                {
                    statusCode = (int)HttpStatusCode.InternalServerError;
                    logger.Error(ex.SourceException);
                }

                KissLog.Http.HttpResponse httpResponse = HttpResponseFactory.Create(context.Response, contentLength);
                httpResponse.SetStatusCode(statusCode);

                logger.DataContainer.HttpProperties.SetResponse(httpResponse);

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

                    responseStream.MirrorStream.Dispose();
                }

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

                KissLog.InternalHelpers.WrapInTryCatch(() =>
                {
                    NotifyListeners.NotifyFlush.Notify(loggers.ToArray());
                });
            }
        }
Exemple #6
0
 public void CreateResponsePropertiesThrowsExceptionForNullArgument()
 {
     KissLog.Http.HttpResponse httpResponse = null;
     var result = PayloadFactory.Create(httpResponse);
 }