Skip to content

Commit f5699c8

Browse files
authored
Include redaction
1 parent 805cb1a commit f5699c8

File tree

1 file changed

+87
-0
lines changed
  • aspnetcore/fundamentals/http-logging

1 file changed

+87
-0
lines changed

aspnetcore/fundamentals/http-logging/index.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ HTTP logging ***can reduce the performance of an app***, especially when logging
3333

3434
> [!WARNING]
3535
> HTTP logging can potentially log personally identifiable information (PII). Consider the risk and avoid logging sensitive information.
36+
> For more information about redaction, check [redacting sensitive data](#redacting-sensitive-data)
3637
3738
## Enable HTTP logging
3839

@@ -205,3 +206,89 @@ The following list shows the order of precedence for logging configuration:
205206
:::moniker-end
206207

207208
[!INCLUDE[](~/fundamentals/http-logging/includes/index-6-7.md)]
209+
210+
## Redacting sensitive data
211+
212+
Http logging with redaction can be enabled by calling <xref:Microsoft.Extensions.DependencyInjection.HttpLoggingServiceCollectionExtensions.AddHttpLoggingRedaction>.
213+
214+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet6&highlight=6)]
215+
216+
For more information about .NET's data redaction library, see [Data redaction in .NET](data-redaction.md).
217+
218+
## Logging redaction options
219+
220+
To configure options for logging with redaction, call <xref:Microsoft.Extensions.DependencyInjection.HttpLoggingServiceCollectionExtensions.AddHttpLoggingRedaction> in `Program.cs`, using the lambda to configure <xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions>.
221+
222+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions)]
223+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/MyTaxonomyClassifications.cs)]
224+
225+
Given this configuration, this is the expected output:
226+
227+
```output
228+
info: Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware[9]
229+
Request and Response:
230+
server.address: localhost:61361
231+
Path: /
232+
http.request.header.accept:
233+
Protocol: HTTP/2
234+
Method: GET
235+
Scheme: https
236+
http.response.header.content-type:
237+
StatusCode: 200
238+
Duration: 8.4684
239+
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
240+
Request finished HTTP/2 GET https://localhost:61361/ - 200 - text/plain;+charset=utf-8 105.5334ms
241+
```
242+
243+
> [!NOTE]
244+
> Request path `/home` is not logged because it is included in `ExcludePathStartsWith` property.
245+
> `http.request.header.accept` and `http.response.header.content-type` were redacted by the <xref:Microsoft.Extensions.Compliance.Redaction.ErasingRedactor>.
246+
247+
### `RequestPathLoggingMode`
248+
249+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.RequestPathLoggingMode> determines how the request path is logged, whether `Formatted` or `Structured`.
250+
251+
* <xref:Microsoft.AspNetCore.Diagnostics.Logging.IncomingPathLoggingMode.Formatted> logs the request path without parameters.
252+
* <xref:Microsoft.AspNetCore.Diagnostics.Logging.IncomingPathLoggingMode.Structured> logs the request path with parameters included.
253+
254+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=9)]
255+
256+
### `RequestPathParameterRedactionMode`
257+
258+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.RequestPathParameterRedactionMode> specifies how route parameters in the request path should be redacted, whether `Strict` or `None`.
259+
260+
* <xref:Microsoft.AspNetCore.Diagnostics.Logging.HttpRouteParameterRedactionMode.Strict>: request route parameters are considered as sensitive and are redacted by default.
261+
* <<xref:Microsoft.AspNetCore.Diagnostics.Logging.HttpRouteParameterRedactionMode.None>: request route parameters are considered as non-sensitive and logged as-is by default.
262+
263+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=8)]
264+
265+
### `RequestHeadersDataClasses`
266+
267+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.RequestHeadersDataClasses> maps request headers to their data classification, which determines how they are redacted.
268+
269+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=10)]
270+
271+
### `ResponseHeadersDataClasses`
272+
273+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.ResponseHeadersDataClasses> similar to <xref:responseheadersdataclasses>, but for response headers.
274+
275+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=11)]
276+
277+
### `RouteParameterDataClasses`
278+
279+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.RouteParameterDataClasses> maps route parameters to their data classification.
280+
281+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=12,13,14,15)]
282+
283+
### `ExcludePathStartsWith`
284+
285+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.ExcludePathStartsWith> specifies paths that should be excluded from logging entirely.
286+
287+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=16,17)]
288+
289+
### `IncludeUnmatchedRoutes`
290+
291+
<xref:Microsoft.AspNetCore.Diagnostics.Logging.LoggingRedactionOptions.IncludeUnmatchedRoutes> allows you to report unmatched routes.
292+
If set to true, it will log whole path of routes not identified by ASP.NET Routing instead of logging `Unknown` value for path attribute.
293+
294+
[!code-csharp[](~/fundamentals/http-logging/samples/8.x/Program.cs?name=snippet_redactionOptions&highlight=18)]

0 commit comments

Comments
 (0)