Skip to content

Commit

Permalink
Do not force filter switch names with a leading $ in OnFilterSwitchCr…
Browse files Browse the repository at this point in the history
…eated callback
  • Loading branch information
0xced committed May 8, 2023
1 parent d22b9fe commit 7f5c27f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration, options)
.CreateLogger();
ILoggingFilterSwitch filterSwitch = filterSwitches["$filterSwitch"];
ILoggingFilterSwitch filterSwitch = filterSwitches["filterSwitch"];
```

### Nested configuration sections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void ProcessFilterSwitchDeclarations()
SetFilterSwitch(throwOnError: true);
SubscribeToFilterExpressionChanges();

var referenceName = _resolutionContext.AddFilterSwitch(switchName, filterSwitch);
_resolutionContext.ReaderOptions.OnFilterSwitchCreated?.Invoke(referenceName, filterSwitch);
_resolutionContext.AddFilterSwitch(switchName, filterSwitch);
_resolutionContext.ReaderOptions.OnFilterSwitchCreated?.Invoke(switchName, filterSwitch);

void SubscribeToFilterExpressionChanges()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)

/// <summary>
/// Called when a log filter switch is created while reading the <c>Serilog:FilterSwitches</c> section of the configuration.
/// The switch name includes the leading <c>$</c> character.
/// </summary>
public Action<string, ILoggingFilterSwitch>? OnFilterSwitchCreated { get; init; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ public string AddLevelSwitch(string levelSwitchName, LoggingLevelSwitch levelSwi
return referenceName;
}

public string AddFilterSwitch(string filterSwitchName, LoggingFilterSwitchProxy filterSwitch)
public void AddFilterSwitch(string filterSwitchName, LoggingFilterSwitchProxy filterSwitch)
{
if (filterSwitchName == null) throw new ArgumentNullException(nameof(filterSwitchName));
if (filterSwitch == null) throw new ArgumentNullException(nameof(filterSwitch));
var referenceName = ToSwitchReference(filterSwitchName);
_declaredFilterSwitches[referenceName] = filterSwitch;
return referenceName;
}

string ToSwitchReference(string switchName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,19 +1548,16 @@ public void TestLogLevelSwitchesCallback(string switchName)
Assert.Equal(LogEventLevel.Debug, systemThreading.MinimumLevel);
}

[Theory]
[InlineData("$switch1", "$switch2")]
[InlineData("$switch1", "switch2")]
[InlineData("switch1", "$switch2")]
[InlineData("switch1", "switch2")]
public void TestLogFilterSwitchesCallback(string switch1Name, string switch2Name)
[Fact]
public void TestLogFilterSwitchesCallback()
{
var json = $$"""
// language=json
var json = """
{
'Serilog': {
'FilterSwitches': {
'{{switch1Name}}': 'Prop = 1',
'{{switch2Name}}': 'Prop = 2'
"Serilog": {
"FilterSwitches": {
"switch1": "Prop = 1",
"$switch2": "Prop = 2"
}
}
}
Expand All @@ -1572,7 +1569,7 @@ public void TestLogFilterSwitchesCallback(string switch1Name, string switch2Name

Assert.Equal(2, switches.Count);

var switch1 = Assert.Contains("$switch1", switches);
var switch1 = Assert.Contains("switch1", switches);
Assert.Equal("Prop = 1", switch1.Expression);

var switch2 = Assert.Contains("$switch2", switches);
Expand Down

0 comments on commit 7f5c27f

Please sign in to comment.