diff --git a/src/Middleware/HttpLogging/src/BufferingStream.cs b/src/Middleware/HttpLogging/src/BufferingStream.cs index 8fabe1006d2e..53fa1c8cbfa1 100644 --- a/src/Middleware/HttpLogging/src/BufferingStream.cs +++ b/src/Middleware/HttpLogging/src/BufferingStream.cs @@ -59,7 +59,8 @@ public string GetString(Encoding? encoding) if (encoding == null) { - _logger.UnrecognizedMediaType(); + // This method is used only for the response body + _logger.UnrecognizedMediaType("response"); return ""; } diff --git a/src/Middleware/HttpLogging/src/HttpLoggingExtensions.cs b/src/Middleware/HttpLogging/src/HttpLoggingExtensions.cs index 8e812b790ee6..a5f04101c3c2 100644 --- a/src/Middleware/HttpLogging/src/HttpLoggingExtensions.cs +++ b/src/Middleware/HttpLogging/src/HttpLoggingExtensions.cs @@ -29,6 +29,9 @@ public static void ResponseLog(this ILogger logger, HttpResponseLog responseLog) [LoggerMessage(5, LogLevel.Debug, "Decode failure while converting body.", EventName = "DecodeFailure")] public static partial void DecodeFailure(this ILogger logger, Exception ex); - [LoggerMessage(6, LogLevel.Debug, "Unrecognized Content-Type for body.", EventName = "UnrecognizedMediaType")] - public static partial void UnrecognizedMediaType(this ILogger logger); + [LoggerMessage(6, LogLevel.Debug, "Unrecognized Content-Type for {Name} body.", EventName = "UnrecognizedMediaType")] + public static partial void UnrecognizedMediaType(this ILogger logger, string name); + + [LoggerMessage(7, LogLevel.Debug, "No Content-Type header for {Name} body.", EventName = "NoMediaType")] + public static partial void NoMediaType(this ILogger logger, string name); } diff --git a/src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs b/src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs index 50eba7a015d9..d2417a3022eb 100644 --- a/src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs +++ b/src/Middleware/HttpLogging/src/HttpLoggingMiddleware.cs @@ -105,7 +105,11 @@ private async Task InvokeInternal(HttpContext context) if (options.LoggingFields.HasFlag(HttpLoggingFields.RequestBody)) { - if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType, + if (request.ContentType is null) + { + _logger.NoMediaType("request"); + } + else if (MediaTypeHelpers.TryGetEncodingForMediaType(request.ContentType, options.MediaTypeOptions.MediaTypeStates, out var encoding)) { @@ -119,7 +123,7 @@ private async Task InvokeInternal(HttpContext context) } else { - _logger.UnrecognizedMediaType(); + _logger.UnrecognizedMediaType("request"); } } diff --git a/src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs b/src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs index 41c63fe80479..b80d0db1e92c 100644 --- a/src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs +++ b/src/Middleware/HttpLogging/test/HttpLoggingMiddlewareTests.cs @@ -511,7 +511,7 @@ public async Task RejectedContentTypes(string contentType) await middleware.Invoke(httpContext); Assert.DoesNotContain(TestSink.Writes, w => w.Message.Contains(expected)); - Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for body.")); + Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for request body.")); } [Fact] @@ -836,7 +836,38 @@ public async Task UnrecognizedMediaType() await middleware.Invoke(httpContext); - Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for body.")); + Assert.Contains(TestSink.Writes, w => w.Message.Contains("Unrecognized Content-Type for response body.")); + } + + [Fact] + public async Task NoMediaType() + { + var options = CreateOptionsAccessor(); + options.CurrentValue.LoggingFields = HttpLoggingFields.RequestBody; + var middleware = new HttpLoggingMiddleware( + async c => + { + c.Request.ContentType = null; + var arr = new byte[4096]; + while (true) + { + var res = await c.Request.Body.ReadAsync(arr); + if (res == 0) + { + break; + } + } + }, + options, + LoggerFactory.CreateLogger()); + + var httpContext = new DefaultHttpContext(); + + httpContext.Request.Headers["foo"] = "bar"; + + await middleware.Invoke(httpContext); + + Assert.Contains(TestSink.Writes, w => w.Message.Contains("No Content-Type header for request body.")); } [Fact]