-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
48 lines (40 loc) · 1.73 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Microsoft.Extensions.Compliance.Classification;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddLogging(lb => lb.EnableRedaction())
.AddHostedService<Worker>();
var host = builder.Build();
host.Run();
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.UserLoggedIn(new User("abcd", "Charles", "[email protected]"));
}
await Task.Delay(1000, stoppingToken);
}
}
}
public record User(string Id, [PersonalData] string Name, [PersonalData] string Email);
// logging code that logs the user
public static partial class Log
{
// Error LOGGEN035 : Parameter "user" of logging method "UserLoggedIn" has a sensitive field/property in its type (https://aka.ms/dotnet-extensions-warnings/LOGGEN035)
// Error CS8795 : Partial method 'Log.UserLoggedIn(ILogger, User)' must have an implementation part because it has accessibility modifiers.
[LoggerMessage(LogLevel.Information, "User {User} logged in")]
public static partial void UserLoggedIn(this ILogger logger, [LogProperties] User user);
}
public class PersonalDataAttribute()
: DataClassificationAttribute(new DataClassification("MyTaxonomy", "MyClassification"))
{
// both of those strings are arbitrary identifiers you can pick.
// You would use them later when configuring redaction to set the policies for your different named classifications.
}