dotnet / dotnet/aspnetcore

After using services.AddHttpLogging(), the ResponseBody cannot be passed to Serilog for file logging.

Open
#58,380 2 comments 0 reactions 0 assignees View on GitHub
area-middleware
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

I reviewed the source code and found that adding "ResponseBody" to the logContext's "Parameters" occurs after the interceptor's "OnResponseAsync" method is executed. I want to access the "ResponseBody" within this method and add it to the diagnostic context for logging with Serilog, but I have found that this is currently not achievable.
![Image](https://github.com/user-attachments/assets/7a36fcfb-583e-4a87-9b34-bc71375a22c1)

I expect to add the "ResponseBody" to the diagnostic context in the red box area in the diagram, so that it can be provided to Serilog for file logging, like in the line diagnosticContext.Set("RequestBody", requestBody);
![Image](https://github.com/user-attachments/assets/4a966af6-8250-4101-aecf-440d19bb1686)

The log template content for Serilog is like this:
![Image](https://github.com/user-attachments/assets/637a7837-1fea-453c-950b-13553c2a47d6)

### Expected Behavior

In the SampleHttpLoggingInterceptor's OnResponseAsync method, read the ResponseBody from the logContext's Parameters, and then add it to the diagnostic context using diagnosticContext.Set("RequestBody", requestBody); to provide it to Serilog for file logging.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

.net 8.0

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.