Open WeihanLi opened 3 years ago
Thanks for reporting this, we will investigate. It's interesting that we only log the headers if the body is empty.
The response headers are logged in your first example just not all of them.
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
Response:
StatusCode: 200
Content-Type: application/json; charset=utf-8
The missing headers are all ones added dynamically by the server when finalizing the response.
Date: [Redacted]
Server: Kestrel
Transfer-Encoding: chunked
The difference you observe between the scenarios is because of when the headers are logged. When body logging is enabled the header logging happens during the first body write, before the server generates those automatic values. When body logging is disabled the headers aren't logged until the end of the request.
Possible fix: when body logging is enabled, log the headers just after the first write rather than before it. That will let them finish generating first.
Priority: Low. While this is inconsistent, these automatic headers are less important than app headers in most scenarios.
@Tratcher thanks for your investigation, currently, we may log before the request is handled, not sure if it's a good idea to log only when the request had been handled, and I think maybe it's better to add a try-finally clause to ensure the logs would be logged even when there's an exception
Thanks for contacting us.
We're moving this issue to the .NET 8 Planning
milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.
I'm also experience this issue, but in my case it's headers that are added in the HttpResponse.OnStarting
callback.
These headers are logged when Response Body logging is off, but not otherwise.
The callback is set up in Middleware which is after the HttpLoggingMiddleware
and before the ASP.Net Controller pipeline.
Describe the bug
When I use the
HttpLoggingMiddleware
to log the response body, the response header would not be logged as expected, not know if this is by design, guessing that it may caused by https://github.com/dotnet/aspnetcore/blob/v6.0.0-rc.1.21452.15/src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs#L172To Reproduce
Just create a api project with
dotnet new webapi -n HttpLoggingMiddlewareSample --no-openapi --no-https
, then addUseHttpLogging()
to the request pipeline, and customize the logging option, modified the sample likes follows:sample project: https://github.com/WeihanLi/SamplesInPractice/blob/master/net6sample/HttpLoggingMiddlewareSample/Program.cs
When I specific the logging fields with
All
, the response headers are not been logged, I got logs like below:while when I commented the
LoggingFields
config, the response headers would be logged, I get logs as follows:Exceptions (if any)
The response headers should be logged
Further technical details
dotnet --info