Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IHttpRequestFeature.Path in RequestLoggingMiddleware to log RequestPath #265

Merged
merged 18 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 _includeRawTargetPath;
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>();
_includeRawTargetPath = options.IncludeRawTargetPath;
}

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

/// <summary>
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

/// </summary>
public bool IncludeRawTargetPath { get; set; } = true;

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