Skip to content

Commit 70e4f0b

Browse files
committed
Add test for support Filter.With()
Turns out support for `Filter.With(ILogEventFilter filter)` was already there since commit 4424523
1 parent 0b3b739 commit 70e4f0b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,5 +991,33 @@ public void EnrichWithIsAppliedWithCustomEnricher()
991991
Assert.NotNull(evt);
992992
Assert.True(evt.Properties.ContainsKey("ThreadId"), "Event should have enriched property ThreadId");
993993
}
994+
995+
[Fact]
996+
public void FilterWithIsAppliedWithCustomFilter()
997+
{
998+
LogEvent evt = null;
999+
1000+
var json = $@"{{
1001+
""Serilog"": {{
1002+
""Using"": [""TestDummies""],
1003+
""Filter"": [
1004+
{{
1005+
""Name"": ""With"",
1006+
""Args"": {{
1007+
""filter"": ""{typeof(DummyAnonymousUserFilter).AssemblyQualifiedName}""
1008+
}}
1009+
}}]
1010+
}}
1011+
}}";
1012+
1013+
var log = ConfigFromJson(json)
1014+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
1015+
.CreateLogger();
1016+
1017+
log.ForContext("User", "anonymous").Write(Some.InformationEvent());
1018+
Assert.Null(evt);
1019+
log.ForContext("User", "the user").Write(Some.InformationEvent());
1020+
Assert.NotNull(evt);
1021+
}
9941022
}
9951023
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+

2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace TestDummies
6+
{
7+
public class DummyAnonymousUserFilter : ILogEventFilter
8+
{
9+
public bool IsEnabled(LogEvent logEvent)
10+
{
11+
if (logEvent.Properties.ContainsKey("User"))
12+
{
13+
if (logEvent.Properties["User"] is ScalarValue sv)
14+
{
15+
if (sv.Value is string s && s == "anonymous")
16+
{
17+
return false;
18+
}
19+
}
20+
}
21+
22+
return true;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)