@@ -4,7 +4,8 @@ public class LogQueryBuilder :
44 IMessageRuleBuilder ,
55 IExceptionRuleBuilder ,
66 IPropertiesRuleBuilder ,
7- ILevelRuleBuilder
7+ ILevelRuleBuilder ,
8+ IFilterExecutor
89{
910 private ConditionGroup _currentGroup = new ( ) ;
1011 private readonly List < ConditionGroup > _groups = [ ] ;
@@ -14,7 +15,7 @@ public class LogQueryBuilder :
1415 public IPropertiesRuleBuilder Properties => this ;
1516 public ILevelRuleBuilder Level => this ;
1617
17- public LogQueryBuilder ( )
18+ private LogQueryBuilder ( )
1819 {
1920 _groups . Add ( _currentGroup ) ;
2021 }
@@ -28,21 +29,7 @@ public ILogQueryBuilder Or()
2829 return this ;
2930 }
3031
31- // public LogQueryBuilder And(LogEntryPredicate predicate)
32- // {
33- // _currentGroup.Conditions.Add(new Condition(predicate));
34- // return this;
35- // }
36-
37- // public LogQueryBuilder Or(LogEntryPredicate predicate)
38- // {
39- // _currentGroup = new ConditionGroup(isOrGroup: true);
40- // _groups.Add(_currentGroup);
41- // _currentGroup.Conditions.Add(new Condition(predicate));
42- // return this;
43- // }
44-
45- public bool Evaluate ( LogEntry entry )
32+ public bool Evaluate ( LogEvent entry )
4633 {
4734 foreach ( var group in _groups )
4835 {
@@ -61,7 +48,7 @@ public bool Evaluate(LogEntry entry)
6148 return false ;
6249 }
6350
64- private bool EvaluateCondition ( object condition , LogEntry entry )
51+ private bool EvaluateCondition ( object condition , LogEvent entry )
6552 {
6653 return condition switch
6754 {
@@ -75,44 +62,46 @@ private bool EvaluateCondition(object condition, LogEntry entry)
7562
7663 #region IMessageRuleBuilder
7764
78- ILogQueryBuilder IMessageRuleBuilder . Contains ( string key )
65+ ILogQueryBuilder IMessageRuleBuilder . Contains ( string substring )
7966 {
80- var condition = new Condition ( e => e . RenderedMessage ? . Contains ( key ) ?? false ) ;
67+ var condition = new Condition ( e => e . RenderMessage ( ) . Contains ( substring ) ) ;
8168 _currentGroup . Conditions . Add ( condition ) ;
8269 return this ;
8370 }
8471
8572 ILogQueryBuilder IMessageRuleBuilder . NotContains ( string substring )
8673 {
87- var condition = new Condition ( e => ! e . RenderedMessage ? . Contains ( substring ) ?? false ) ;
74+ var condition = new Condition ( e => ! e . RenderMessage ( ) . Contains ( substring ) ) ;
8875 _currentGroup . Conditions . Add ( condition ) ;
8976 return this ;
9077 }
9178
9279 ILogQueryBuilder IMessageRuleBuilder . Equals ( string substring , StringComparison comparison )
9380 {
94- var condition = new Condition ( e => e . RenderedMessage ? . Equals ( substring , comparison ) ?? false ) ;
81+ var condition = new Condition ( e => e . RenderMessage ( ) . Equals ( substring , comparison ) ) ;
9582 _currentGroup . Conditions . Add ( condition ) ;
9683 return this ;
9784 }
9885
9986 ILogQueryBuilder IMessageRuleBuilder . NotEquals ( string substring , StringComparison comparison )
10087 {
101- var condition = new Condition ( e => ! e . RenderedMessage ? . Equals ( substring , comparison ) ?? false ) ;
88+ var condition = new Condition ( e => ! e . RenderMessage ( ) . Equals ( substring , comparison ) ) ;
10289 _currentGroup . Conditions . Add ( condition ) ;
10390 return this ;
10491 }
10592
10693 ILogQueryBuilder IMessageRuleBuilder . Null ( )
10794 {
108- var condition = new Condition ( e => e . RenderedMessage is null ) ;
95+ // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
96+ var condition = new Condition ( e => e . RenderMessage ( ) is null ) ;
10997 _currentGroup . Conditions . Add ( condition ) ;
11098 return this ;
11199 }
112100
113101 ILogQueryBuilder IMessageRuleBuilder . NotNull ( )
114102 {
115- var condition = new Condition ( e => e . RenderedMessage is not null ) ;
103+ // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
104+ var condition = new Condition ( e => e . RenderMessage ( ) is not null ) ;
116105 _currentGroup . Conditions . Add ( condition ) ;
117106 return this ;
118107 }
@@ -121,30 +110,30 @@ ILogQueryBuilder IMessageRuleBuilder.NotNull()
121110
122111 #region IExceptionRuleBuilder
123112
124- ILogQueryBuilder IExceptionRuleBuilder . Contains ( string key )
113+ ILogQueryBuilder IExceptionRuleBuilder . Contains ( string substring )
125114 {
126- var condition = new Condition ( e => e . Exception ? . Contains ( key ) ?? false ) ;
115+ var condition = new Condition ( e => e . Exception ? . Message . Contains ( substring ) ?? false ) ;
127116 _currentGroup . Conditions . Add ( condition ) ;
128117 return this ;
129118 }
130119
131120 ILogQueryBuilder IExceptionRuleBuilder . NotContains ( string substring )
132121 {
133- var condition = new Condition ( e => ! e . Exception ? . Contains ( substring ) ?? false ) ;
122+ var condition = new Condition ( e => ! e . Exception ? . Message . Contains ( substring ) ?? false ) ;
134123 _currentGroup . Conditions . Add ( condition ) ;
135124 return this ;
136125 }
137126
138127 ILogQueryBuilder IExceptionRuleBuilder . Equals ( string substring , StringComparison comparison )
139128 {
140- var condition = new Condition ( e => e . Exception ? . Equals ( substring , comparison ) ?? false ) ;
129+ var condition = new Condition ( e => e . Exception ? . Message . Equals ( substring , comparison ) ?? false ) ;
141130 _currentGroup . Conditions . Add ( condition ) ;
142131 return this ;
143132 }
144133
145134 ILogQueryBuilder IExceptionRuleBuilder . NotEquals ( string substring , StringComparison comparison )
146135 {
147- var condition = new Condition ( e => ! e . Exception ? . Equals ( substring , comparison ) ?? false ) ;
136+ var condition = new Condition ( e => ! e . Exception ? . Message . Equals ( substring , comparison ) ?? false ) ;
148137 _currentGroup . Conditions . Add ( condition ) ;
149138 return this ;
150139 }
@@ -167,59 +156,49 @@ ILogQueryBuilder IExceptionRuleBuilder.NotNull()
167156
168157 #region IPropertiesRuleBuilder
169158
170- ILogQueryBuilder IPropertiesRuleBuilder . Contains ( string key )
159+ ILogQueryBuilder IPropertiesRuleBuilder . Contains ( string substring )
171160 {
172- var condition = new Condition ( e => e . Properties ? . ContainsKey ( key ) ?? false ) ;
161+ var condition = new Condition ( e => e . Properties . ContainsKey ( substring ) ) ;
173162 _currentGroup . Conditions . Add ( condition ) ;
174163 return this ;
175164 }
176165
177166 ILogQueryBuilder IPropertiesRuleBuilder . NotContains ( string key )
178167 {
179- var condition = new Condition ( e => ! e . Properties ? . ContainsKey ( key ) ?? false ) ;
168+ var condition = new Condition ( e => ! e . Properties . ContainsKey ( key ) ) ;
180169 _currentGroup . Conditions . Add ( condition ) ;
181170 return this ;
182171 }
183172
184173 ILogQueryBuilder IPropertiesRuleBuilder . Equals ( string key , string value , StringComparison comparison )
185174 {
186- var condition = new Condition ( e =>
187- {
188- if ( ! e . Properties ? . ContainsKey ( key ) ?? true )
189- {
190- return false ;
191- }
192-
193- return e . Properties ! [ key ] . Equals ( value , comparison ) ;
194- } ) ;
175+ var condition = new Condition (
176+ e => e . Properties . TryGetValue ( key , out var prop ) &&
177+ prop . ToString ( ) . Equals ( value , comparison ) ) ;
195178 _currentGroup . Conditions . Add ( condition ) ;
196179 return this ;
197180 }
198181
199182 ILogQueryBuilder IPropertiesRuleBuilder . NotEquals ( string key , string value , StringComparison comparison )
200183 {
201- var condition = new Condition ( e =>
202- {
203- if ( ! e . Properties ? . ContainsKey ( key ) ?? true )
204- {
205- return false ;
206- }
207-
208- return e . Properties ! [ key ] . Equals ( value , comparison ) ;
209- } ) ;
184+ var condition = new Condition (
185+ e => ! ( e . Properties . TryGetValue ( key , out var prop ) &&
186+ prop . ToString ( ) . Equals ( value , comparison ) ) ) ;
210187 _currentGroup . Conditions . Add ( condition ) ;
211188 return this ;
212189 }
213190
214191 ILogQueryBuilder IPropertiesRuleBuilder . Null ( )
215192 {
193+ // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
216194 var condition = new Condition ( e => e . Properties is null ) ;
217195 _currentGroup . Conditions . Add ( condition ) ;
218196 return this ;
219197 }
220198
221199 ILogQueryBuilder IPropertiesRuleBuilder . NotNull ( )
222200 {
201+ // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
223202 var condition = new Condition ( e => e . Properties is not null ) ;
224203 _currentGroup . Conditions . Add ( condition ) ;
225204 return this ;
@@ -231,12 +210,32 @@ ILogQueryBuilder IPropertiesRuleBuilder.NotNull()
231210
232211 ILogQueryBuilder ILevelRuleBuilder . InRange ( LogEventLevel min , LogEventLevel max )
233212 {
234- throw new NotImplementedException ( ) ;
213+ var minInt = ( int ) min ;
214+ var maxInt = ( int ) max ;
215+
216+ var condition = new Condition ( e =>
217+ {
218+ var logLevelInt = ( int ) e . Level ;
219+ return minInt <= logLevelInt && logLevelInt <= maxInt ;
220+ } ) ;
221+
222+ _currentGroup . Conditions . Add ( condition ) ;
223+ return this ;
235224 }
236225
237226 ILogQueryBuilder ILevelRuleBuilder . NotInRange ( LogEventLevel min , LogEventLevel max )
238227 {
239- throw new NotImplementedException ( ) ;
228+ var minInt = ( int ) min ;
229+ var maxInt = ( int ) max ;
230+
231+ var condition = new Condition ( e =>
232+ {
233+ var logLevelInt = ( int ) e . Level ;
234+ return ! ( logLevelInt >= minInt && logLevelInt <= maxInt ) ;
235+ } ) ;
236+
237+ _currentGroup . Conditions . Add ( condition ) ;
238+ return this ;
240239 }
241240
242241 ILogQueryBuilder ILevelRuleBuilder . Equals ( LogEventLevel level )
@@ -254,4 +253,9 @@ ILogQueryBuilder ILevelRuleBuilder.NotEquals(LogEventLevel level)
254253 }
255254
256255 #endregion
256+
257+ public static LogQueryBuilder Create ( )
258+ {
259+ return new LogQueryBuilder ( ) ;
260+ }
257261}
0 commit comments