File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
Serilog.Settings.Configuration.Tests Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -991,5 +991,33 @@ public void EnrichWithIsAppliedWithCustomEnricher()
991
991
Assert . NotNull ( evt ) ;
992
992
Assert . True ( evt . Properties . ContainsKey ( "ThreadId" ) , "Event should have enriched property ThreadId" ) ;
993
993
}
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
+ }
994
1022
}
995
1023
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments