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

Separate EnableRedaction and EnableEnrichment #4838

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public ExtendedLoggerFactory(
_filterOptionsChangeTokenRegistration = filterOptions.OnChange(RefreshFilters);
RefreshFilters(filterOptions.CurrentValue);

if (enrichmentOptions == null)
geeknoid marked this conversation as resolved.
Show resolved Hide resolved
{
// enrichmentOptions is only present if EnableEnrichment was called, so if it's null
// then ignore all the supplied enrichers, we're not doing enrichment
#pragma warning disable S1226
enrichers = [];
staticEnrichers = [];
#pragma warning restore S1226
}

_enrichers = enrichers.Select<ILogEnricher, Action<IEnrichmentTagCollector>>(e => e.Enrich).ToArray();
_enrichmentOptionsChangeTokenRegistration = enrichmentOptions?.OnChange(UpdateEnrichmentOptions);
_redactionOptionsChangeTokenRegistration = redactionOptions?.OnChange(UpdateRedactionOptions);
Expand All @@ -80,7 +90,7 @@ public ExtendedLoggerFactory(
}

_staticTags = [.. tags];
Config = ComputeConfig(enrichmentOptions?.CurrentValue, redactionOptions?.CurrentValue);
Config = ComputeConfig(enrichmentOptions?.CurrentValue ?? new(), redactionOptions?.CurrentValue ?? new() { ApplyDiscriminator = false });
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ namespace Microsoft.Extensions.Logging.Test;

public static class ExtendedLoggerTests
{
[Fact]
public static void Basic()
[Theory]
[CombinatorialData]
public static void FeatureEnablement(bool enableRedaction, bool enableEnrichment)
{
const string Category = "C1";

Expand All @@ -39,11 +40,14 @@ public static void Basic()
RedactionFormat = "REDACTED<{0}>",
});

var enrichmentOptions = enableEnrichment ? new StaticOptionsMonitor<LoggerEnrichmentOptions>(new()) : null;
var redactionOptions = enableRedaction ? new StaticOptionsMonitor<LoggerRedactionOptions>(new() { ApplyDiscriminator = false }) : null;

using var lf = new ExtendedLoggerFactory(
providers: new[] { provider },
filterOptions: new StaticOptionsMonitor<LoggerFilterOptions>(new()),
enrichmentOptions: new StaticOptionsMonitor<LoggerEnrichmentOptions>(new()),
redactionOptions: new StaticOptionsMonitor<LoggerRedactionOptions>(new() { ApplyDiscriminator = false }),
enrichmentOptions: enrichmentOptions,
redactionOptions: redactionOptions,
enrichers: new[] { enricher },
staticEnrichers: new[] { staticEnricher },
redactorProvider: redactorProvider,
Expand Down Expand Up @@ -74,18 +78,45 @@ public static void Basic()
Assert.Null(snap[0].Exception);
Assert.Equal(new EventId(0), snap[0].Id);
Assert.Equal("MSG0", snap[0].Message);
Assert.Equal("EV1", snap[0].GetStructuredStateValue("EK1"));
Assert.Equal("SEV1", snap[0].GetStructuredStateValue("SEK1"));

if (enableEnrichment)
{
Assert.Equal("SEV1", snap[0].GetStructuredStateValue("SEK1"));
Assert.Equal("EV1", snap[0].GetStructuredStateValue("EK1"));
}
else
{
Assert.Null(snap[0].GetStructuredStateValue("SEK1"));
Assert.Null(snap[0].GetStructuredStateValue("EK1"));
}

Assert.Equal(Category, snap[1].Category);
Assert.Null(snap[1].Exception);
Assert.Equal(new EventId(2, "ID2"), snap[1].Id);
Assert.Equal("MSG2", snap[1].Message);
Assert.Equal("PV2", snap[1].GetStructuredStateValue("PK2"));
Assert.Equal("REDACTED<PV3>", snap[1].GetStructuredStateValue("PK3"));

if (enableRedaction)
{
Assert.Equal("REDACTED<PV3>", snap[1].GetStructuredStateValue("PK3"));
}
else
{
Assert.Equal("PV3", snap[1].GetStructuredStateValue("PK3"));
}

Assert.Null(snap[1].GetStructuredStateValue("PK4"));
Assert.Equal("EV1", snap[1].GetStructuredStateValue("EK1"));
Assert.Equal("SEV1", snap[1].GetStructuredStateValue("SEK1"));

if (enableEnrichment)
{
Assert.Equal("SEV1", snap[1].GetStructuredStateValue("SEK1"));
Assert.Equal("EV1", snap[1].GetStructuredStateValue("EK1"));
}
else
{
Assert.Null(snap[1].GetStructuredStateValue("SEK1"));
Assert.Null(snap[1].GetStructuredStateValue("EK1"));
}
}

[Theory]
Expand Down