From 8fd539d711feafbc5133df37b152393627eaedbc Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Date: Thu, 30 Apr 2020 16:11:39 -0300 Subject: [PATCH 1/2] Fallback RequestPath for when IHttpRequestFeature.RawTarteg is empty instead of null --- .../AspNetCore/RequestLoggingMiddleware.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs index e6c17f1..db2eb0c 100644 --- a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs +++ b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs @@ -106,7 +106,13 @@ static double GetElapsedMilliseconds(long start, long stop) static string GetPath(HttpContext httpContext) { - return httpContext.Features.Get()?.RawTarget ?? httpContext.Request.Path.ToString(); + var requestPath = httpContext.Features.Get()?.RawTarget; + if (string.IsNullOrWhiteSpace(requestPath)) + { + requestPath = httpContext.Request.Path.ToString(); + } + + return requestPath; } } } From 4fea46555139083e9f0ec8481c48e9afd14540d0 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Date: Thu, 30 Apr 2020 20:27:50 -0300 Subject: [PATCH 2/2] Using string.IsNullOrEmpty / Adding extra comments --- .../AspNetCore/RequestLoggingMiddleware.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs index db2eb0c..08419e5 100644 --- a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs +++ b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs @@ -106,8 +106,13 @@ static double GetElapsedMilliseconds(long start, long stop) static string GetPath(HttpContext httpContext) { + /* + In some cases, like when running integration tests with WebApplicationFactory + the RawTarget returns an empty string instead of null, in that case we can't use + ?? as fallback. + */ var requestPath = httpContext.Features.Get()?.RawTarget; - if (string.IsNullOrWhiteSpace(requestPath)) + if (string.IsNullOrEmpty(requestPath)) { requestPath = httpContext.Request.Path.ToString(); }