Skip to content

Conversation

@pwelter34
Copy link

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:

  • Added IncludeRequestBody, RequestBodyContentTypes, and RequestBodyContentMaxLength options to RequestLoggingOptions, allowing users to opt-in to logging request bodies, specify which content types to log, and set a maximum length for logged bodies.
  • Implemented logic in RequestLoggingMiddleware to 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 IncludeRequestBody is enabled, including content verification for JSON payloads. Enhanced RequestLoggingMiddleware to log request bodies based on new RequestLoggingOptions. Introduced CollectRequestBody method to handle body collection, ensuring logging only for specified content types and within a maximum length. Updated RequestLoggingOptions with IncludeRequestBody, RequestBodyContentTypes, and RequestBodyContentMaxLength properties for configuration.

  • Added tests in SerilogWebHostBuilderExtensionsTests to verify request body logging functionality and ensure backward compatibility with .NET versions.

Enhanced `RequestLoggingMiddleware` to log request bodies based on new `RequestLoggingOptions`. Introduced `CollectRequestBody` method to handle body collection, ensuring logging only for specified content types and within a maximum length. Updated `RequestLoggingOptions` with `IncludeRequestBody`, `RequestBodyContentTypes`, and `RequestBodyContentMaxLength` properties for configuration.

Added tests in `SerilogWebHostBuilderExtensionsTests` to verify request body logging functionality and ensure backward compatibility with .NET versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant