Example #1
0
        public void ReturnsNullStrategyWhenStreamIsDisposed()
        {
            var stream = new Mock <Stream>();

            stream.Setup(p => p.CanRead).Returns(false);

            ILogResponseBodyStrategy strategy = LogResponseBodyStrategyFactory.Create(stream.Object, Encoding.UTF8, new Logger());

            Assert.IsInstanceOfType(strategy, typeof(NullLogResponseBody));
        }
Example #2
0
        public void ReturnLogResponseBodySizeTooLargeExceptionWhenContentSizeIsGreaterThanMaximumAllowedFileSize()
        {
            var stream = new Mock <Stream>();

            stream.Setup(p => p.CanRead).Returns(true);
            stream.Setup(p => p.Length).Returns(Constants.MaximumAllowedFileSizeInBytes + 1);

            ILogResponseBodyStrategy strategy = LogResponseBodyStrategyFactory.Create(stream.Object, Encoding.UTF8, new Logger());

            Assert.IsInstanceOfType(strategy, typeof(LogResponseBodySizeTooLargeException));
        }
Example #3
0
        public void DoesNotReturnLogResponseBodySizeTooLargeExceptionWhenContentSizeIsLteThanMaximumAllowedFileSize(long contentLength)
        {
            var stream = new Mock <Stream>();

            stream.Setup(p => p.CanRead).Returns(true);
            stream.Setup(p => p.Length).Returns(contentLength);

            Logger logger = new Logger(url: "/");

            logger.DataContainer.HttpProperties.SetResponse(new KissLog.Http.HttpResponse(new KissLog.Http.HttpResponse.CreateOptions
            {
                Properties = new KissLog.Http.ResponseProperties(new KissLog.Http.ResponseProperties.CreateOptions
                {
                    Headers = new List <KeyValuePair <string, string> >
                    {
                        new KeyValuePair <string, string>("Content-Type", "application/json")
                    }
                })
            }));

            ILogResponseBodyStrategy strategy = LogResponseBodyStrategyFactory.Create(stream.Object, Encoding.UTF8, logger);

            Assert.IsNotInstanceOfType(strategy, typeof(LogResponseBodySizeTooLargeException));
        }
Example #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());
            });
        }
Example #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());
                });
            }
        }
Example #6
0
        public void ThrowsExceptionWhenLoggerIsNull()
        {
            var stream = new Mock <Stream>();

            LogResponseBodyStrategyFactory.Create(stream.Object, Encoding.UTF8, null);
        }
Example #7
0
 public void ThrowsExceptionWhenStreamIsNull()
 {
     LogResponseBodyStrategyFactory.Create(null, Encoding.UTF8, new Logger());
 }