Add request body logging to middleware #400
Open
+161
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds support for logging HTTP request bodies in the
RequestLoggingMiddleware, with configurable options for when and how much of the body to log. It also introduces tests to ensure this functionality works as expected and remains off by default unless enabled.Request body logging enhancements:
IncludeRequestBody,RequestBodyContentTypes, andRequestBodyContentMaxLengthoptions toRequestLoggingOptions, allowing users to opt-in to logging request bodies, specify which content types to log, and set a maximum length for logged bodies.RequestLoggingMiddlewareto read, buffer, and log the request body based on the new options, with support for both .NET 5+ and older frameworks. This includes handling content type filtering and size limits, and ensures the request body stream is reset for further processing.Testing improvements:
Added tests to verify that the request body is not logged by default and is logged correctly when
IncludeRequestBodyis enabled, including content verification for JSON payloads. EnhancedRequestLoggingMiddlewareto log request bodies based on newRequestLoggingOptions. IntroducedCollectRequestBodymethod to handle body collection, ensuring logging only for specified content types and within a maximum length. UpdatedRequestLoggingOptionswithIncludeRequestBody,RequestBodyContentTypes, andRequestBodyContentMaxLengthproperties for configuration.Added tests in
SerilogWebHostBuilderExtensionsTeststo verify request body logging functionality and ensure backward compatibility with .NET versions.