diff --git a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs index e6c17f1..08419e5 100644 --- a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs +++ b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs @@ -106,7 +106,18 @@ static double GetElapsedMilliseconds(long start, long stop) static string GetPath(HttpContext httpContext) { - return httpContext.Features.Get()?.RawTarget ?? httpContext.Request.Path.ToString(); + /* + 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.IsNullOrEmpty(requestPath)) + { + requestPath = httpContext.Request.Path.ToString(); + } + + return requestPath; } } }