Skip to content

Commit 7f5c27f

Browse files
committed
Do not force filter switch names with a leading $ in OnFilterSwitchCreated callback
1 parent d22b9fe commit 7f5c27f

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ var logger = new LoggerConfiguration()
288288
.ReadFrom.Configuration(configuration, options)
289289
.CreateLogger();
290290
291-
ILoggingFilterSwitch filterSwitch = filterSwitches["$filterSwitch"];
291+
ILoggingFilterSwitch filterSwitch = filterSwitches["filterSwitch"];
292292
```
293293

294294
### Nested configuration sections

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ void ProcessFilterSwitchDeclarations()
7575
SetFilterSwitch(throwOnError: true);
7676
SubscribeToFilterExpressionChanges();
7777

78-
var referenceName = _resolutionContext.AddFilterSwitch(switchName, filterSwitch);
79-
_resolutionContext.ReaderOptions.OnFilterSwitchCreated?.Invoke(referenceName, filterSwitch);
78+
_resolutionContext.AddFilterSwitch(switchName, filterSwitch);
79+
_resolutionContext.ReaderOptions.OnFilterSwitchCreated?.Invoke(switchName, filterSwitch);
8080

8181
void SubscribeToFilterExpressionChanges()
8282
{

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReaderOptions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public ConfigurationReaderOptions() : this(dependencyContext: null)
7979

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

src/Serilog.Settings.Configuration/Settings/Configuration/ResolutionContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ public string AddLevelSwitch(string levelSwitchName, LoggingLevelSwitch levelSwi
6262
return referenceName;
6363
}
6464

65-
public string AddFilterSwitch(string filterSwitchName, LoggingFilterSwitchProxy filterSwitch)
65+
public void AddFilterSwitch(string filterSwitchName, LoggingFilterSwitchProxy filterSwitch)
6666
{
6767
if (filterSwitchName == null) throw new ArgumentNullException(nameof(filterSwitchName));
6868
if (filterSwitch == null) throw new ArgumentNullException(nameof(filterSwitch));
6969
var referenceName = ToSwitchReference(filterSwitchName);
7070
_declaredFilterSwitches[referenceName] = filterSwitch;
71-
return referenceName;
7271
}
7372

7473
string ToSwitchReference(string switchName)

test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,19 +1548,16 @@ public void TestLogLevelSwitchesCallback(string switchName)
15481548
Assert.Equal(LogEventLevel.Debug, systemThreading.MinimumLevel);
15491549
}
15501550

1551-
[Theory]
1552-
[InlineData("$switch1", "$switch2")]
1553-
[InlineData("$switch1", "switch2")]
1554-
[InlineData("switch1", "$switch2")]
1555-
[InlineData("switch1", "switch2")]
1556-
public void TestLogFilterSwitchesCallback(string switch1Name, string switch2Name)
1551+
[Fact]
1552+
public void TestLogFilterSwitchesCallback()
15571553
{
1558-
var json = $$"""
1554+
// language=json
1555+
var json = """
15591556
{
1560-
'Serilog': {
1561-
'FilterSwitches': {
1562-
'{{switch1Name}}': 'Prop = 1',
1563-
'{{switch2Name}}': 'Prop = 2'
1557+
"Serilog": {
1558+
"FilterSwitches": {
1559+
"switch1": "Prop = 1",
1560+
"$switch2": "Prop = 2"
15641561
}
15651562
}
15661563
}
@@ -1572,7 +1569,7 @@ public void TestLogFilterSwitchesCallback(string switch1Name, string switch2Name
15721569

15731570
Assert.Equal(2, switches.Count);
15741571

1575-
var switch1 = Assert.Contains("$switch1", switches);
1572+
var switch1 = Assert.Contains("switch1", switches);
15761573
Assert.Equal("Prop = 1", switch1.Expression);
15771574

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

0 commit comments

Comments
 (0)