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()); }); } }