aspnetcore: HttpLogger does not log RequestBody
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
HttpLogging does not log the RequestBody when configured to do so.
Expected Behavior
A log statement from HttpLogger with the actual body sent
Steps To Reproduce
using Microsoft.AspNetCore.HttpLogging;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(c =>
{
c.LoggingFields = HttpLoggingFields.RequestBody |
HttpLoggingFields.RequestPath |
HttpLoggingFields.ResponseBody;
});
var app = builder.Build();
app.UseHttpLogging();
app.MapPost("/", () => "Hello POST!");
app.Run();
Exceptions (if any)
Any request having a body towards the POST endpoint at root results in a log statement that says the body is empty when it’s not:
POST http://localhost:5145/ HTTP/1.1
User-Agent: vscode-restclient
Content-Type: text/plain
accept-encoding: gzip, deflate
content-length: 3
some text
HTTP/1.1 200 OK
Connection: close
Content-Type: text/plain; charset=utf-8
Date: Tue, 27 Jun 2023 22:13:05 GMT
Server: Kestrel
Transfer-Encoding: chunked
Hello POST!
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:5145/ text/plain 3
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
Request:
PathBase:
Path: /
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'HTTP: POST /'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'HTTP: POST /'
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[3]
RequestBody: 👈 👈
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[4]
ResponseBody: Hello POST!
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 POST http://localhost:5145/ text/plain 3 - 200 - text/plain;+charset=utf-8 0.4329ms
.NET Version
7.0.203
Anything else?
Microsoft.AspNetCore.App 7.0.5 VSCode / Rider
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 20 (12 by maintainers)
Commits related to this issue
- Note when the request body was not fully logged #49063 — committed to Tratcher/aspnetcore by Tratcher 10 months ago
- HttpLogging redaction and enrichment (#50163) * Proof of concept for extending http logging * Feedback, cleanup * Nits * Add duration * Fix log, test * Pool log contexts * API revie... — committed to dotnet/aspnetcore by Tratcher 10 months ago
We should change the log to indicate the full request body wasn’t consumed. Self-documenting :->
Confirmed working on my machine ™️ on 7.0.8, and in a container (non-azure) using 7.0.8 image.
I recomend DrainAsync instead of StreamReader, it doesn’t need to store or decode the data. https://github.com/dotnet/aspnetcore/blob/1e86b6cd87163153337cbb5555fa727264eceb2e/src/Http/WebUtilities/src/StreamHelperExtensions.cs#L24
I think this is a duplicate of https://github.com/dotnet/aspnetcore/issues/47216 which was fixed in 7.0.7 by https://github.com/dotnet/aspnetcore/pull/47811. Can you try updating?
I think we should update our documentation to mention this.