You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Logging is a common source of accidental data exposure. Sensitive information such as personal data, credentials, or financial details should never be written to logs in plain text. To prevent this, always use redaction when logging potentially sensitive data.
190
+
191
+
### Steps for logging sensitive data
192
+
193
+
1.**Install the telemetry extensions package**: Install [Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) to be able to use the extended logger to enable redaction feature.
194
+
2.**Set up redaction**: Integrate redactors with your logging pipeline by calling the <xref:Microsoft.Extensions.DependencyInjection.RedactionServiceCollectionExtensions.AddRedaction(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method, to automatically sanitize or mask sensitive fields before they are written to logs.
195
+
3.**Identify sensitive fields**: Know which data in your application is sensitive and requires protection, and mark them with appropriate data classification.
196
+
4.**Review log output**: Regularly audit your logs to ensure no sensitive data is exposed.
197
+
198
+
### Example: Redacting data in logs
199
+
200
+
When using [Microsoft.Extensions.Logging](https://www.nuget.org/packages/Microsoft.Extensions.Logging), you can combine redaction with logging as follows:
201
+
202
+
```csharp
203
+
usingMicrosoft.Extensions.Telemetry;
204
+
usingMicrosoft.Extensions.Compliance.Redaction;
205
+
206
+
varservices=newServiceCollection();
207
+
services.AddLogging(builder=>
208
+
{
209
+
// Enable redaction.
210
+
builder.EnableRedaction();
211
+
});
212
+
213
+
services.AddRedaction(builder=>
214
+
{
215
+
// configure redactors for your data classifications
Copy file name to clipboardExpand all lines: docs/core/extensions/logger-message-generator.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -367,6 +367,64 @@ Consider the example logging output when using the `JsonConsole` formatter:
367
367
}
368
368
```
369
369
370
+
## Redacting sensitive information in logs
371
+
372
+
When logging sensitive data, it's important to prevent accidental exposure. Even with compile-time generated logging methods, logging raw sensitive values can lead to data leaks and compliance issues.
373
+
374
+
The [Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) library provides advanced logging and telemetry enrichment capabilities for .NET applications. It extends the logging pipeline to automatically apply redaction to classified data when writing logs. It enables you to enforce data protection policies throughout your application by integrating redaction into your logging workflow. It is built for applications needing sophisticated telemetry and logging insights.
375
+
376
+
To enable redaction, use the [Microsoft.Extensions.Compliance.Redaction](https://www.nuget.org/packages/Microsoft.Extensions.Compliance.Redaction) library. This library provides **redactors**—components that transform sensitive data (for example, by erasing, masking, or hashing it) so that it is safe to output. Redactors are selected based on **data classification**, which lets you label data according to its sensitivity (such as personal, private, or public).
377
+
378
+
To use redaction with source-generated logging methods, you should:
379
+
380
+
1. Classify your sensitive data using a data classification system.
381
+
2. Register and configure redactors for each classification in your DI container.
382
+
3. Enable redaction in the logging pipeline.
383
+
4. Check your logs to ensure no sensitive data is exposed.
384
+
385
+
For example, if you have a log message that has a parameter that is considered private:
This approach ensures that only redacted data is logged, even when using compile-time generated logging APIs. You can use different redactors for different data types or classifications, and update your redaction logic centrally.
424
+
425
+
For more details about how to classify your data, see [Data classification in .NET](data-classification.md).
426
+
For more details about redaction and redactors, see [Data redaction in .NET](data-redaction.md).
427
+
370
428
## Summary
371
429
372
430
With the advent of C# source generators, writing highly performant logging APIs is much easier. Using the source generator approach has several key benefits:
@@ -388,4 +446,6 @@ Additionally, there are benefits over manually using <xref:Microsoft.Extensions.
388
446
-[Logging in .NET](logging.md)
389
447
-[High-performance logging in .NET](high-performance-logging.md)
0 commit comments