Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HttpLogging redaction and enrichment (#50163)
* Proof of concept for extending http logging * Feedback, cleanup * Nits * Add duration * Fix log, test * Pool log contexts * API review * Cleanup, TimeProvider * Duration * Write temp files to the current dir, it might be on a different/faster drive. * Cleanup * Tests * Fix CopyTo/Async #49989 * Apply suggestions from code review Co-authored-by: Sébastien Ros <sebastienros@gmail.com> * Clarify code check error * Comments, copyto, TryOverride * Usings * Code review, comments, tests * Restructuring * Note when the request body was not fully logged #49063 * Powershell fix? * Revert "Clarify code check error" * Feedback * Feedback * Combine logs, log even if an exception happens * Clarify * Don't log empty response body * Getting cozy * Simplify Duration * Less pooling * Clean up comments * Even less re-use * Clean up Unshipped.txt * Re-fix Unshipped --------- Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
- Loading branch information
1 parent
7f749ea
commit af54f0b
Showing
20 changed files
with
1,721 additions
and
539 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/Middleware/HttpLogging/samples/HttpLogging.Sample/SampleHttpLoggingInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.HttpLogging; | ||
|
||
namespace HttpLogging.Sample; | ||
|
||
internal sealed class SampleHttpLoggingInterceptor : IHttpLoggingInterceptor | ||
{ | ||
public ValueTask OnRequestAsync(HttpLoggingInterceptorContext logContext) | ||
{ | ||
// Compare to ExcludePathStartsWith | ||
if (!logContext.HttpContext.Request.Path.StartsWithSegments("/api")) | ||
{ | ||
logContext.LoggingFields = HttpLoggingFields.None; | ||
} | ||
|
||
// Don't enrich if we're not going to log any part of the request | ||
if (!logContext.IsAnyEnabled(HttpLoggingFields.Request)) | ||
{ | ||
return default; | ||
} | ||
|
||
if (logContext.TryDisable(HttpLoggingFields.RequestPath)) | ||
{ | ||
RedactPath(logContext); | ||
} | ||
|
||
if (logContext.TryDisable(HttpLoggingFields.RequestHeaders)) | ||
{ | ||
RedactRequestHeaders(logContext); | ||
} | ||
|
||
EnrichRequest(logContext); | ||
|
||
return default; | ||
} | ||
|
||
private void RedactRequestHeaders(HttpLoggingInterceptorContext logContext) | ||
{ | ||
foreach (var header in logContext.HttpContext.Request.Headers) | ||
{ | ||
logContext.AddParameter(header.Key, "RedactedHeader"); // TODO: Redact header value | ||
} | ||
} | ||
|
||
private void RedactResponseHeaders(HttpLoggingInterceptorContext logContext) | ||
{ | ||
foreach (var header in logContext.HttpContext.Response.Headers) | ||
{ | ||
logContext.AddParameter(header.Key, "RedactedHeader"); // TODO: Redact header value | ||
} | ||
} | ||
|
||
public ValueTask OnResponseAsync(HttpLoggingInterceptorContext logContext) | ||
{ | ||
// Don't enrich if we're not going to log any part of the response | ||
if (!logContext.IsAnyEnabled(HttpLoggingFields.Response)) | ||
{ | ||
return default; | ||
} | ||
|
||
if (logContext.TryDisable(HttpLoggingFields.ResponseHeaders)) | ||
{ | ||
RedactResponseHeaders(logContext); | ||
} | ||
|
||
EnrichResponse(logContext); | ||
|
||
return default; | ||
} | ||
|
||
private void EnrichResponse(HttpLoggingInterceptorContext logContext) | ||
{ | ||
logContext.AddParameter("ResponseEnrichment", "Stuff"); | ||
} | ||
|
||
private void EnrichRequest(HttpLoggingInterceptorContext logContext) | ||
{ | ||
logContext.AddParameter("RequestEnrichment", "Stuff"); | ||
} | ||
|
||
private void RedactPath(HttpLoggingInterceptorContext logContext) | ||
{ | ||
logContext.AddParameter(nameof(logContext.HttpContext.Request.Path), "RedactedPath"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.