Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround to fix compiler errors #1

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions LogRedactionDemo.SimpleWorker/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
using System.Text.Json;
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Compliance.Redaction;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddLogging(lb => lb.EnableRedaction())
.AddHostedService<Worker>();
builder.Services
.AddHostedService<Worker>()
.AddLogging(lb =>
{
lb.EnableRedaction();

// Log structured logs as JSON to console so we can see the actual structured data
lb.AddJsonConsole(o => o.JsonWriterOptions = new JsonWriterOptions { Indented = true });

// AddRedaction make sure the redactor provider is hooked up so that the logger can get a redactor
// bey default the ErasingRedactor is added as the fallback redactor which erases all data marked with any
// DataClassificationAttribute
// lb.Services.AddRedaction();

// This is how you can configure redactors in more detail
lb.Services.AddRedaction(rb =>
rb.SetRedactor<ErasingRedactor>(
new DataClassificationSet(new DataClassification("MyTaxonomy", "MyClassification")))
.SetFallbackRedactor<NullRedactor>());
});

var host = builder.Build();
host.Run();

Expand All @@ -20,22 +41,32 @@ 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);
public class User
{
public User(string Id, string Name, string Email)
{
this.Id = Id;
this.Name = Name;
this.Email = Email;
}

public string Id { get; }

[PersonalData] public string Name { get; }

[PersonalData] public string Email { get; }
}

// 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);
}
Expand Down