title: Getting empty response on ASP.NET Core middleware on exception
body: <p>I am trying to create a middleware that can log the response body as well as manage exception globally and I was succeeded about that. My problem is that the custom message that I put on exception it's not showing on the response.</p>
  <p>Middleware Code 01:</p>
  <pre><code>public async Task Invoke(HttpContext context)
  {
      context.Request.EnableRewind();
          
      var originalBodyStream = context.Response.Body;
      using (var responseBody = new MemoryStream())
      {
          try
          {
              context.Response.Body = responseBody;
              await next(context);
  
              context.Response.Body.Seek(0, SeekOrigin.Begin);
              var response = await new StreamReader(context.Response.Body).ReadToEndAsync();
              context.Response.Body.Seek(0, SeekOrigin.Begin);
  
              // Process log
               var log = new LogMetadata();
               log.RequestMethod = context.Request.Method;
               log.RequestUri = context.Request.Path.ToString();
               log.ResponseStatusCode = context.Response.StatusCode;
               log.ResponseTimestamp = DateTime.Now;
               log.ResponseContentType = context.Response.ContentType;
               log.ResponseContent = response;
               // Keep Log to text file
               CustomLogger.WriteLog(log);
  
              await responseBody.CopyToAsync(originalBodyStream);
          }
          catch (Exception ex)
          {
              context.Response.ContentType = &quot;application/json&quot;;
              context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
              var jsonObject = JsonConvert.SerializeObject(My Custom Model);
              await context.Response.WriteAsync(jsonObject, Encoding.UTF8);
              return;
          }
      }
  }
  </code></pre>
  <p>If I write my middleware like that, my custom exception is working fine but I unable to log my response body.</p>
  <p>Middleware Code 02:</p>
  <pre><code> public async Task Invoke(HttpContext context)
    {
      context.Request.EnableRewind();
  
      try
      {
          await next(context);
      }
      catch (Exception ex)
      {
          context.Response.ContentType = &quot;application/json&quot;;
          context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
          var jsonObject = JsonConvert.SerializeObject(My Custom Model);
          await context.Response.WriteAsync(jsonObject, Encoding.UTF8);
          return;
      }
  }
  </code></pre>
  <p>My Controller Action :</p>
  <pre><code>    [HttpGet]
      public ActionResult&lt;IEnumerable&lt;string&gt;&gt; Get()
      {
          throw new Exception(&quot;Exception Message&quot;);
      }
  </code></pre>
  <p>Now I want to show my exception message with my middleware 01, but it doesn't work but its work on my middleware 02.</p>
  <p>So my observation is the problem is occurring for reading the context response. Is there anything I have missed in my middleware 01 code?</p>
  <p>Is there any better way to serve my purpose that log the response body as well as manage exception globally?</p>