title: Exception thrown but middleware context response is a 200, but response to client is 500
body: <p>I've got a middleware class that logs path, time taken &amp; response codes.</p>
  <pre><code>public async Task InvokeAsync(HttpContext context)
  {
    var sw = Stopwatch.StartNew();
    try
    {
      await _next(context); // Controller throws an exception, un caught. 
    }
    finally
    {
      // at this point, context.Response.StatusCode is 200. but the response to client is 500.
      _logger.LogInformation(&quot;{Path} - {StatusCode} - {TimeTaken}&quot;, 
          context.Request.Path, context.Response.StatusCode, sw.ElapsedMilliseconds);
    }
  }
  </code></pre>
  <p>When an error occurs, <code>context.Response.StatusCode</code> has a value of 200. I'd have expected it to be 500 (even though I'm not explicitly returning <code>InternalServerError</code>).</p>
  <p>If I get a 400, 404, or genuine 200 the status code is correct.</p>
  <p>But when an exception is thrown, the middleware still gets a 200 in context.Response.StatusCode, but the consuming client gets a 500 as expected.</p>
  <p>Thanks,
  David</p>