@@ -33,6 +33,7 @@ class RequestLoggingMiddleware
3333 readonly Action < IDiagnosticContext , HttpContext > _enrichDiagnosticContext ;
3434 readonly Func < HttpContext , double , Exception , LogEventLevel > _getLevel ;
3535 readonly ILogger _logger ;
36+ readonly bool _includeQueryInRequestPath ;
3637 static readonly LogEventProperty [ ] NoProperties = new LogEventProperty [ 0 ] ;
3738
3839 public RequestLoggingMiddleware ( RequestDelegate next , DiagnosticContext diagnosticContext , RequestLoggingOptions options )
@@ -45,6 +46,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost
4546 _enrichDiagnosticContext = options . EnrichDiagnosticContext ;
4647 _messageTemplate = new MessageTemplateParser ( ) . Parse ( options . MessageTemplate ) ;
4748 _logger = options . Logger ? . ForContext < RequestLoggingMiddleware > ( ) ;
49+ _includeQueryInRequestPath = options . IncludeQueryInRequestPath ;
4850 }
4951
5052 // ReSharper disable once UnusedMember.Global
@@ -91,7 +93,7 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector
9193 var properties = collectedProperties . Concat ( new [ ]
9294 {
9395 new LogEventProperty ( "RequestMethod" , new ScalarValue ( httpContext . Request . Method ) ) ,
94- new LogEventProperty ( "RequestPath" , new ScalarValue ( GetPath ( httpContext ) ) ) ,
96+ new LogEventProperty ( "RequestPath" , new ScalarValue ( GetPath ( httpContext , _includeQueryInRequestPath ) ) ) ,
9597 new LogEventProperty ( "StatusCode" , new ScalarValue ( statusCode ) ) ,
9698 new LogEventProperty ( "Elapsed" , new ScalarValue ( elapsedMs ) )
9799 } ) ;
@@ -107,14 +109,16 @@ static double GetElapsedMilliseconds(long start, long stop)
107109 return ( stop - start ) * 1000 / ( double ) Stopwatch . Frequency ;
108110 }
109111
110- static string GetPath ( HttpContext httpContext )
112+ static string GetPath ( HttpContext httpContext , bool includeQueryInRequestPath )
111113 {
112114 /*
113115 In some cases, like when running integration tests with WebApplicationFactory<T>
114- the RawTarget returns an empty string instead of null, in that case we can't use
116+ the Path returns an empty string instead of null, in that case we can't use
115117 ?? as fallback.
116118 */
117- var requestPath = httpContext . Features . Get < IHttpRequestFeature > ( ) ? . RawTarget ;
119+ var requestPath = includeQueryInRequestPath
120+ ? httpContext . Features . Get < IHttpRequestFeature > ( ) ? . RawTarget
121+ : httpContext . Features . Get < IHttpRequestFeature > ( ) ? . Path ;
118122 if ( string . IsNullOrEmpty ( requestPath ) )
119123 {
120124 requestPath = httpContext . Request . Path . ToString ( ) ;
0 commit comments