Skip to content

Commit 2cdbe9b

Browse files
mariamgergesR9 Fundamentals
andauthored
Adding sections about logging sensitive information (#46492)
* Adding sections about logging sensitive information * fix * fixing links * fixing docs * comments * updated * comments --------- Co-authored-by: R9 Fundamentals <[email protected]>
1 parent e42a868 commit 2cdbe9b

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

docs/core/extensions/data-redaction.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,53 @@ public sealed class StarRedactorProvider : IRedactorProvider
183183
public Redactor GetRedactor(DataClassificationSet classifications) => _starRedactor;
184184
}
185185
```
186+
187+
## Logging sensitive information
188+
189+
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+
using Microsoft.Extensions.Telemetry;
204+
using Microsoft.Extensions.Compliance.Redaction;
205+
206+
var services = new ServiceCollection();
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
216+
builder.SetRedactor<StarRedactor>(MyTaxonomyClassifications.Private);
217+
});
218+
// Use annotations to mark sensitive data.
219+
// For example, apply the Private classification to SSN data.
220+
[LoggerMessage(0, LogLevel.Information, "User SSN: {SSN}")]
221+
public static partial void LogPrivateInformation(
222+
this ILogger logger,
223+
[MyTaxonomyClassifications.Private] string SSN);
224+
225+
public void TestLogging()
226+
{
227+
LogPrivateInformation("MySSN");
228+
}
229+
```
230+
231+
The output should be like this:
232+
233+
`User SSN: *****`
234+
235+
This ensures that sensitive data is redacted before being logged, reducing the risk of data leaks.

docs/core/extensions/logger-message-generator.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,64 @@ Consider the example logging output when using the `JsonConsole` formatter:
367367
}
368368
```
369369

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:
386+
387+
```csharp
388+
[LoggerMessage(0, LogLevel.Information, "User SSN: {SSN}")]
389+
public static partial void LogPrivateInformation(
390+
this ILogger logger,
391+
[MyTaxonomyClassifications.Private] string SSN);
392+
```
393+
394+
You will need to have a setting similar to this:
395+
396+
```csharp
397+
using Microsoft.Extensions.Telemetry;
398+
using Microsoft.Extensions.Compliance.Redaction;
399+
400+
var services = new ServiceCollection();
401+
services.AddLogging(builder =>
402+
{
403+
// Enable redaction.
404+
builder.EnableRedaction();
405+
});
406+
407+
services.AddRedaction(builder =>
408+
{
409+
// configure redactors for your data classifications
410+
builder.SetRedactor<StarRedactor>(MyTaxonomyClassifications.Private);
411+
});
412+
413+
public void TestLogging()
414+
{
415+
LogPrivateInformation("MySSN");
416+
}
417+
```
418+
419+
The output should be like this:
420+
421+
`User SSN: *****`
422+
423+
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+
370428
## Summary
371429

372430
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.
388446
- [Logging in .NET](logging.md)
389447
- [High-performance logging in .NET](high-performance-logging.md)
390448
- [Console log formatting](console-log-formatter.md)
449+
- [Data redaction in .NET](data-redaction.md)
450+
- [Data classification in .NET](data-classification.md)
391451
- [NuGet: Microsoft.Extensions.Logging.Abstractions](https://www.nuget.org/packages/microsoft.extensions.logging.abstractions)

0 commit comments

Comments
 (0)