public async Task Invoke(HttpContext context) { var internalTrace = Guid.NewGuid(); _loggingHelper.LogHttpRequest(context.Request, internalTrace); var originalBodyStream = context.Response.Body; using (var responseBody = new MemoryStream()) { context.Response.Body = responseBody; await _next(context); context.Response.Body.Position = 0; await responseBody.CopyToAsync(originalBodyStream); _loggingHelper.LogHttpResponse(context.Response, internalTrace); } context.Response.Body = originalBodyStream; }