HttpLoggingMiddleware does not log all response headers when log response body
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### 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

### To Reproduce
Just create a api project with `dotnet new webapi -n HttpLoggingMiddlewareSample --no-openapi --no-https`, then add `UseHttpLogging()` to the request pipeline, and customize the logging option, modified the sample likes follows:
``` csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddHttpLogging(options =>
{
options.LoggingFields = Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All;
options.RequestHeaders.Add("Cache-Control");
options.ResponseHeaders.Add("Server");
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpLogging();
app.MapControllers();
app.Run();
```
sample project:
When I specific the logging fields with `All`, the response headers are not been logged, I got logs like below:
```
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
Request:
Protocol: HTTP/1.1
Method: GET
Scheme: http
PathBase:
Path: /weatherforecast
Host: localhost:5084
User-Agent: dotnet-HTTPie/0.1.1
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
Response:
StatusCode: 200
Content-Type: application/json; charset=utf-8
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[4]
ResponseBody: [{"date":"2021-09-25T22:37:45.0463903+08:00","temperatureC":-2,"temperatureF":29,"summary":"Mild"},{"date":"2021-09-26T22:37:45.0498642+08:00","temperatureC":-20,"temperatureF":-3,"summary":"Cool"},{"date":"2021-09-27T22:37:45.0498738+08:00","temperatureC":-20,"temperatureF":-3,"summary":"Freezing"},{"date":"2021-09-28T22:37:45.0498741+08:00","temperatureC":-15,"temperatureF":6,"summary":"Cool"},{"date":"2021-09-29T22:37:45.0498743+08:00","temperatureC":23,"temperatureF":73,"summary":"Balmy"}]
```
while when I commented the `LoggingFields` config, the response headers would be logged, I get logs as follows:
```
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[1]
Request:
Protocol: HTTP/1.1
Method: GET
Scheme: http
PathBase:
Path: /weatherforecast
Host: localhost:5084
User-Agent: dotnet-HTTPie/0.1.1
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[2]
Response:
StatusCode: 200
Content-Type: application/json; charset=utf-8
Date: [Redacted]
Server: Kestrel
Transfer-Encoding: chunked
```
### Exceptions (if any)
The response headers should be logged
### Further technical details
- ASP.NET Core version:.NET 6 RC 1
- The IDE: VS 2022 Preview 4/ dotnet cli(.NET 6 RC1)
- Include the output of `dotnet --info`
```
.NET SDK (reflecting any global.json):
Version: 6.0.100-rc.1.21458.32
Commit: d7c22323c4
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22463
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100-rc.1.21458.32\
Host (useful for support):
Version: 6.0.0-rc.1.21451.13
Commit: d7619cd4b1
.NET SDKs installed:
2.1.802 [C:\Program Files\dotnet\sdk]
2.2.300 [C:\Program Files\dotnet\sdk]
3.1.300 [C:\Program Files\dotnet\sdk]
3.1.301 [C:\Program Files\dotnet\sdk]
3.1.412 [C:\Program Files\dotnet\sdk]
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.100-rc.1.21458.32 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
```
Contributor guide
Assessment
This issue has not been assessed yet.