@@ -33,6 +33,7 @@ class RequestLoggingMiddleware
33
33
readonly Action < IDiagnosticContext , HttpContext > _enrichDiagnosticContext ;
34
34
readonly Func < HttpContext , double , Exception , LogEventLevel > _getLevel ;
35
35
readonly ILogger _logger ;
36
+ readonly bool _includeQueryInRequestPath ;
36
37
static readonly LogEventProperty [ ] NoProperties = new LogEventProperty [ 0 ] ;
37
38
38
39
public RequestLoggingMiddleware ( RequestDelegate next , DiagnosticContext diagnosticContext , RequestLoggingOptions options )
@@ -45,6 +46,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost
45
46
_enrichDiagnosticContext = options . EnrichDiagnosticContext ;
46
47
_messageTemplate = new MessageTemplateParser ( ) . Parse ( options . MessageTemplate ) ;
47
48
_logger = options . Logger ? . ForContext < RequestLoggingMiddleware > ( ) ;
49
+ _includeQueryInRequestPath = options . IncludeQueryInRequestPath ;
48
50
}
49
51
50
52
// ReSharper disable once UnusedMember.Global
@@ -91,7 +93,7 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector
91
93
var properties = collectedProperties . Concat ( new [ ]
92
94
{
93
95
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 ) ) ) ,
95
97
new LogEventProperty ( "StatusCode" , new ScalarValue ( statusCode ) ) ,
96
98
new LogEventProperty ( "Elapsed" , new ScalarValue ( elapsedMs ) )
97
99
} ) ;
@@ -107,14 +109,16 @@ static double GetElapsedMilliseconds(long start, long stop)
107
109
return ( stop - start ) * 1000 / ( double ) Stopwatch . Frequency ;
108
110
}
109
111
110
- static string GetPath ( HttpContext httpContext )
112
+ static string GetPath ( HttpContext httpContext , bool includeQueryInRequestPath )
111
113
{
112
114
/*
113
115
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
115
117
?? as fallback.
116
118
*/
117
- var requestPath = httpContext . Features . Get < IHttpRequestFeature > ( ) ? . RawTarget ;
119
+ var requestPath = includeQueryInRequestPath
120
+ ? httpContext . Features . Get < IHttpRequestFeature > ( ) ? . RawTarget
121
+ : httpContext . Features . Get < IHttpRequestFeature > ( ) ? . Path ;
118
122
if ( string . IsNullOrEmpty ( requestPath ) )
119
123
{
120
124
requestPath = httpContext . Request . Path . ToString ( ) ;
0 commit comments