Skip to content

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

Open
@zengqinglei

Description

Is there an existing issue for this?

  • 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

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

The log template content for Serilog is like this:
Image

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

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlesware

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions