Skip to content

Commit

Permalink
Use IHttpRequestFeature.Path in RequestLoggingMiddleware to log Reque…
Browse files Browse the repository at this point in the history
…stPath (#265)


Co-authored-by: Ivan Maximov <sungam3r@yandex.ru>
  • Loading branch information
jarronshih and sungam3r authored Nov 12, 2021
1 parent 76f4a51 commit 8e91c9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Serilog.AspNetCore/AspNetCore/RequestLoggingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RequestLoggingMiddleware
readonly Action<IDiagnosticContext, HttpContext> _enrichDiagnosticContext;
readonly Func<HttpContext, double, Exception, LogEventLevel> _getLevel;
readonly ILogger _logger;
readonly bool _includeQueryInRequestPath;
static readonly LogEventProperty[] NoProperties = new LogEventProperty[0];

public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnosticContext, RequestLoggingOptions options)
Expand All @@ -45,6 +46,7 @@ public RequestLoggingMiddleware(RequestDelegate next, DiagnosticContext diagnost
_enrichDiagnosticContext = options.EnrichDiagnosticContext;
_messageTemplate = new MessageTemplateParser().Parse(options.MessageTemplate);
_logger = options.Logger?.ForContext<RequestLoggingMiddleware>();
_includeQueryInRequestPath = options.IncludeQueryInRequestPath;
}

// ReSharper disable once UnusedMember.Global
Expand Down Expand Up @@ -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))
});
Expand All @@ -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<T>
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<IHttpRequestFeature>()?.RawTarget;
var requestPath = includeQueryInRequestPath
? httpContext.Features.Get<IHttpRequestFeature>()?.RawTarget
: httpContext.Features.Get<IHttpRequestFeature>()?.Path;
if (string.IsNullOrEmpty(requestPath))
{
requestPath = httpContext.Request.Path.ToString();
Expand Down
6 changes: 6 additions & 0 deletions src/Serilog.AspNetCore/AspNetCore/RequestLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ static LogEventLevel DefaultGetLevel(HttpContext ctx, double _, Exception ex) =>
/// </summary>
public ILogger Logger { get; set; }

/// <summary>
/// Include the full URL query string in the <c>RequestPath</c> property
/// that is attached to request log events. The default is <c>true</c>.
/// </summary>
public bool IncludeQueryInRequestPath { get; set; } = true;

/// <summary>
/// Constructor
/// </summary>
Expand Down

0 comments on commit 8e91c9a

Please sign in to comment.