Skip to content

Commit acd7024

Browse files
authored
Merge pull request #191 from rsantosdev/dev
Fallback RequestPath for when IHttpRequestFeature.RawTarteg is empty
2 parents d9087b6 + 4fea465 commit acd7024

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,18 @@ static double GetElapsedMilliseconds(long start, long stop)
106106

107107
static string GetPath(HttpContext httpContext)
108108
{
109-
return httpContext.Features.Get<IHttpRequestFeature>()?.RawTarget ?? httpContext.Request.Path.ToString();
109+
/*
110+
In some cases, like when running integration tests with WebApplicationFactory<T>
111+
the RawTarget returns an empty string instead of null, in that case we can't use
112+
?? as fallback.
113+
*/
114+
var requestPath = httpContext.Features.Get<IHttpRequestFeature>()?.RawTarget;
115+
if (string.IsNullOrEmpty(requestPath))
116+
{
117+
requestPath = httpContext.Request.Path.ToString();
118+
}
119+
120+
return requestPath;
110121
}
111122
}
112123
}

0 commit comments

Comments
 (0)