Skip to content

HttpLogging drops Request/Response bodies if Stream has been closed #61489

Open
@ExtraClock

Description

@ExtraClock

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The HttpLoggingMiddleware logs empty request/response bodies in case the subsequent middleware closes the corresponding stream. For example, CoreWCF does that for the BasicHttpBinding.

info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[3]
      RequestBody: [Only partially consumed by app]
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[4]
      ResponseBody:

Expected Behavior

Request and Response bodies are being logged whether the stream has been closed or not.

Steps To Reproduce

  1. Create an empty Web application
  2. Change Logging:LogLevel:Microsoft.AspNetCore to Information in appsettings*.json
  3. Put the following code in the Program.cs
using Microsoft.AspNetCore.HttpLogging;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(options => options.LoggingFields = HttpLoggingFields.All);
var app = builder.Build();

app.UseHttpLogging();

app.Run(async context =>
{
    var buffer = new byte[context.Request.ContentLength!.Value];
    await context.Request.Body.ReadExactlyAsync(buffer, 0, buffer.Length);
    context.Request.Body.Close(); // (1) Close the request body stream
    
    context.Response.ContentType = "text/plain";
    await context.Response.Body.WriteAsync(buffer);
    context.Response.Body.Close(); // (2) Close the response body stream
});

await app.StartAsync();

var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(app.Urls.First());
var response = await httpClient.PostAsync("/", new StringContent("LOOK FOR THIS IN THE LOGS"));
app.Logger.LogInformation("Response received: {response}, body: {body}", response, response.Content.ReadAsStringAsync().GetAwaiter().GetResult());

await app.StopAsync();
  1. Run the app
  2. Observe that HttpLoggingMiddleware doesn't log neither Request nor Response bodies:
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5082
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\immediate\WebApplication2
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5082/ - text/plain;+charset=utf-8 25
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
      Request:
      Protocol: HTTP/1.1
      Method: POST
      Scheme: http
      PathBase: 
      Path: /
      Host: localhost:5082
      Content-Type: text/plain; charset=utf-8
      Content-Length: 25
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
      Response:
      StatusCode: 200
      Content-Type: text/plain
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[3]
      RequestBody: [Only partially consumed by app]
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[4]
      ResponseBody:
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[8]
      Duration: 11.3532ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 POST http://localhost:5082/ - 200 - text/plain 22.4930ms
info: WebApplication2[0]
      Response received: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
      {
        Date: Mon, 14 Apr 2025 13:28:05 GMT
        Server: Kestrel
        Transfer-Encoding: chunked
        Content-Type: text/plain
      }, body: LOOK FOR THIS IN THE LOGS
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...

Process finished with exit code 0.

Exceptions (if any)

No response

.NET Version

10.0.100-preview.4.25207.10

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @ExtraClock

      Issue actions

        HttpLogging drops Request/Response bodies if Stream has been closed · Issue #61489 · dotnet/aspnetcore