diff --git a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs index dee6a5d..cc2d84b 100644 --- a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs +++ b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs @@ -33,6 +33,7 @@ class RequestLoggingMiddleware readonly Action _enrichDiagnosticContext; readonly Func _getLevel; readonly ILogger _logger; + readonly bool _includeQueryInRequestPath; static readonly LogEventProperty[] NoProperties = new LogEventProperty[0]; public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options) @@ -45,6 +46,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost _enrichDiagnosticContext = options.EnrichDiagnosticContext; _messageTemplate = new MessageTemplateParser().Parse(options.MessageTemplate); _logger = options.Logger?.ForContext(); + _includeQueryInRequestPath = options.IncludeQueryInRequestPath; } // ReSharper disable once UnusedMember.Global @@ -91,7 +93,7 @@ bool LogCompletion(HttpContext httpContext, DiagnosticContextCollector collector var properties = collectedProperties.Concat(new[] { new LogEventProperty("RequestMethod", new ScalarValue(httpContext.Request.Method)), - new LogEventProperty("RequestPath", new ScalarValue(GetPath(httpContext))), + new LogEventProperty("RequestPath", new ScalarValue(GetPath(httpContext, _includeQueryInRequestPath))), new LogEventProperty("StatusCode", new ScalarValue(statusCode)), new LogEventProperty("Elapsed", new ScalarValue(elapsedMs)) }); @@ -107,14 +109,16 @@ static double GetElapsedMilliseconds(long start, long stop) return (stop - start) * 1000 / (double)Stopwatch.Frequency; } - static string GetPath(HttpContext httpContext) + static string GetPath(HttpContext httpContext, bool includeQueryInRequestPath) { /* 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 + the Path returns an empty string instead of null, in that case we can't use ?? as fallback. */ - var requestPath = httpContext.Features.Get()?.RawTarget; + var requestPath = includeQueryInRequestPath + ? httpContext.Features.Get()?.RawTarget + : httpContext.Features.Get()?.Path; if (string.IsNullOrEmpty(requestPath)) { requestPath = httpContext.Request.Path.ToString(); diff --git a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs index 8d86582..1c7b434 100644 --- a/src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs +++ b/src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs @@ -68,6 +68,12 @@ static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception ex) => /// public ILogger Logger { get; set; } + /// + /// Include the full URL query string in the RequestPath property + /// that is attached to request log events. The default is true. + /// + public bool IncludeQueryInRequestPath { get; set; } = true; + /// /// Constructor ///