Пример #1
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());
                });
            }
        }
Пример #2
0
        public async Task Invoke(HttpContext context)
        {
            Logger logger = Logger.Factory.Get() as Logger;

            if (logger == null)
            {
                return;
            }

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

            logger.DataContainer.WebProperties = webProperties;

            KissLog.Internal.NotifyListeners.NotifyBeginRequest(webProperties.Request, logger);

            Exception     ex = null;
            Stream        originalBodyStream = context.Response.Body;
            TemporaryFile responseBodyFile   = null;
            long          contentLength      = 0;

            try
            {
                using (var responseStream = new MemoryStream())
                {
                    context.Response.Body = responseStream;

                    await _next(context);

                    responseBodyFile = await ReadResponseAsync(context.Response);

                    contentLength = responseStream.Length;

                    if (CanWriteToResponseBody(context.Response))
                    {
                        await responseStream.CopyToAsync(originalBodyStream);
                    }
                }
            }
            catch (Exception e)
            {
                ex = e;
                throw;
            }
            finally
            {
                context.Response.Body = originalBodyStream;

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

                HttpStatusCode statusCode = (HttpStatusCode)context.Response.StatusCode;

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

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

                response.HttpStatusCode           = statusCode;
                response.Properties.ContentLength = contentLength;

                if (responseBodyFile != null && InternalHelpers.PreFilterShouldLogResponseBody(logger, responseBodyFile, response.Properties))
                {
                    string responseFileName = InternalHelpers.ResponseFileName(response.Properties.Headers);
                    logger.LogFile(responseBodyFile.FileName, responseFileName);
                }

                logger.DataContainer.WebProperties = webProperties;

                responseBodyFile?.Dispose();

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

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