title: How to log the HTTP Response Body in ASP.NET Core 1.0
body: <p>I'm creating a public REST Api using ASP.NET Core 1.0 RC2 and like to log incoming requests and outgoing responses.</p>
  
  <p>I have created a middleware class which is added to the pipeline before the call to app.UseMvc();</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {            
          app.UseIOMiddleware();
          app.UseMvc();            
  }
  </code></pre>
  
  <p>My Middleware class looks like this:</p>
  
  <pre><code>public class IOMiddleware
  {
      private readonly RequestDelegate _next;
  
      public IOMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          LogRequest(context.Request);
  
          await _next.Invoke(context);            
      }
  
      private async void LogRequest(HttpRequest request)
      {
          using (var bodyReader = new StreamReader(request.Body))
          {
              string body = await bodyReader.ReadToEndAsync();
  
              request.Body = new MemoryStream(Encoding.UTF8.GetBytes(body));
  
              System.Diagnostics.Debug.Print(body);
          }
      }
  }
  </code></pre>
  
  <p>I can read the request body stream and rewind it using this example: <a href="https://stackoverflow.com/questions/21805362/rewind-request-body-stream">Rewind request body stream</a>, but I'm not sure how to read the response body as the stream is not readable. </p>
  
  <p>In Web API 2.0 I could have used the HttpResponseMessage.Content.ReadAsByteArrayAsync() method, but how can I accomplish the same thing in ASP.Net Core 1.0 RC2?</p>