Skip to content

Commit

Permalink
feat: exclude logging completely for configured paths
Browse files Browse the repository at this point in the history
by default excludes /hc and /liveness
  • Loading branch information
abhith committed Jun 25, 2020
1 parent a45d9aa commit 490d104
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Samples/AspNetCoreApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseRequestResponseLogging(opt =>
{
opt.ExcludePaths.RequestBody.Add("/secret");
opt.ExcludePaths.ResponseBody.Add("/secret");
opt.Exclude.RequestBody.Add("/secret");
opt.Exclude.ResponseBody.Add("/secret");
});
app.UseApiExceptionHandler();

Expand Down
9 changes: 1 addition & 8 deletions Samples/AspNetCoreApp/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ public RequestResponseLoggingMiddleware(RequestDelegate next, ILoggerFactory log

public async Task Invoke(HttpContext context)
{
await LogRequest(context);
await LogResponse(context);
if (_options.Exclude.Paths.Any(path => context.Request.Path.Value.Contains(path, System.StringComparison.CurrentCultureIgnoreCase)))
{
await _next(context);
}
else
{
await LogRequest(context);
await LogResponse(context);
}
}

private static string ReadStreamInChunks(Stream stream)
Expand All @@ -52,7 +59,7 @@ private static string ReadStreamInChunks(Stream stream)

private async Task LogRequest(HttpContext context)
{
if (_options.ExcludePaths.RequestBody.Any(path => context.Request.Path.Value.Contains(path, System.StringComparison.CurrentCultureIgnoreCase)))
if (_options.Exclude.RequestBody.Any(path => context.Request.Path.Value.Contains(path, System.StringComparison.CurrentCultureIgnoreCase)))
{
_logger.LogInformation("----- Handling HTTP Request {RequestUrl} (***)", context.Request.GetDisplayUrl());
}
Expand All @@ -68,7 +75,7 @@ private async Task LogRequest(HttpContext context)

private async Task LogResponse(HttpContext context)
{
if (_options.ExcludePaths.ResponseBody.Any(path => context.Request.Path.Value.Contains(path, System.StringComparison.CurrentCultureIgnoreCase)))
if (_options.Exclude.ResponseBody.Any(path => context.Request.Path.Value.Contains(path, System.StringComparison.CurrentCultureIgnoreCase)))
{
await _next(context);
_logger.LogInformation("----- Handled HTTP Request {RequestUrl} (***)", context.Request.GetDisplayUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static IApplicationBuilder BuilderWithRequestResponseLogging(IApplicatio
{
opts.EnrichDiagnosticContext = SerilogHelper.EnrichFromRequest;

if (options.ExcludeHealthChecks)
if (options.Exclude.HealthChecks)
{
opts.GetLevel = SerilogHelper.GetLevel(LogEventLevel.Verbose, "Health checks");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

namespace Code.Library.AspNetCore.Middleware
{
public class ExcludePathsInRequestResponseLoggingOptions
public class ExcludeInRequestResponseLoggingOptions
{
public bool HealthChecks { get; set; } = true;

/// <summary>
/// No logs for matching paths
/// </summary>
public ICollection<string> Paths { get; set; } = new List<string>() { "/hc", "/liveness" };

/// <summary>
/// Request body will not be logged for matching paths
/// </summary>
public ICollection<string> RequestBody { get; set; } = new List<string>();

/// <summary>
/// Response body will not be logged for matching paths
/// </summary>
public ICollection<string> ResponseBody { get; set; } = new List<string>();
}

public class RequestResponseLoggingOptions
{
public bool ExcludeHealthChecks { get; set; } = true;
public ExcludePathsInRequestResponseLoggingOptions ExcludePaths { get; set; } = new ExcludePathsInRequestResponseLoggingOptions();
/// <summary>
/// Excluded areas from logging
/// </summary>
public ExcludeInRequestResponseLoggingOptions Exclude { get; set; } = new ExcludeInRequestResponseLoggingOptions();
}
}

0 comments on commit 490d104

Please sign in to comment.