Skip to content

Commit 8e91c9a

Browse files
jarronshihsungam3r
andauthored
Use IHttpRequestFeature.Path in RequestLoggingMiddleware to log RequestPath (#265)
Co-authored-by: Ivan Maximov <[email protected]>
1 parent 76f4a51 commit 8e91c9a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception ex) =>
6868
/// </summary>
6969
public ILogger Logger { get; set; }
7070

71+
/// <summary>
72+
/// Include the full URL query string in the <c>RequestPath</c> property
73+
/// that is attached to request log events. The default is <c>true</c>.
74+
/// </summary>
75+
public bool IncludeQueryInRequestPath { get; set; } = true;
76+
7177
/// <summary>
7278
/// Constructor
7379
/// </summary>

0 commit comments

Comments
 (0)