11
11
using Serilog . Core ;
12
12
using Serilog . Debugging ;
13
13
using Serilog . Events ;
14
+ using System . Linq . Expressions ;
14
15
15
16
namespace Serilog . Settings . Configuration
16
17
{
@@ -33,9 +34,20 @@ public void Configure(LoggerConfiguration loggerConfiguration)
33
34
34
35
ApplyMinimumLevel ( loggerConfiguration ) ;
35
36
ApplyEnrichment ( loggerConfiguration , configurationAssemblies ) ;
37
+ ApplyFilters ( loggerConfiguration , configurationAssemblies ) ;
36
38
ApplySinks ( loggerConfiguration , configurationAssemblies ) ;
37
39
}
38
40
41
+ void ApplyFilters ( LoggerConfiguration loggerConfiguration , Assembly [ ] configurationAssemblies )
42
+ {
43
+ var filterDirective = _configuration . GetSection ( "Filter" ) ;
44
+ if ( filterDirective != null )
45
+ {
46
+ var methodCalls = GetMethodCalls ( filterDirective ) ;
47
+ CallConfigurationMethods ( methodCalls , FindFilterConfigurationMethods ( configurationAssemblies ) , loggerConfiguration . Filter ) ;
48
+ }
49
+ }
50
+
39
51
void ApplySinks ( LoggerConfiguration loggerConfiguration , Assembly [ ] configurationAssemblies )
40
52
{
41
53
var writeToDirective = _configuration . GetSection ( "WriteTo" ) ;
@@ -273,21 +285,20 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IEnumerable<Assem
273
285
return FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
274
286
}
275
287
276
- // Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
277
- internal static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
288
+ internal static IList < MethodInfo > FindFilterConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
278
289
{
279
- return loggerEnrichmentConfiguration . FromLogContext ( ) ;
280
- }
290
+ var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerFilterConfiguration ) ) ;
291
+ if ( configurationAssemblies . Contains ( typeof ( LoggerFilterConfiguration ) . GetTypeInfo ( ) . Assembly ) )
292
+ found . Add ( GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter > ( ( c , f ) => With ( c , f ) ) ) ;
281
293
282
- static readonly MethodInfo SurrogateFromLogContextConfigurationMethod = typeof ( ConfigurationReader )
283
- . GetTypeInfo ( )
284
- . DeclaredMethods . Single ( m => m . Name == "FromLogContext" ) ;
294
+ return found ;
295
+ }
285
296
286
297
internal static IList < MethodInfo > FindEventEnricherConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
287
298
{
288
299
var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
289
300
if ( configurationAssemblies . Contains ( typeof ( LoggerEnrichmentConfiguration ) . GetTypeInfo ( ) . Assembly ) )
290
- found . Add ( SurrogateFromLogContextConfigurationMethod ) ;
301
+ found . Add ( GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object > ( ( c , _ ) => FromLogContext ( c ) ) ) ;
291
302
292
303
return found ;
293
304
}
@@ -303,5 +314,22 @@ internal static IList<MethodInfo> FindConfigurationMethods(IEnumerable<Assembly>
303
314
. Where ( m => m . GetParameters ( ) [ 0 ] . ParameterType == configType )
304
315
. ToList ( ) ;
305
316
}
317
+
318
+ // don't support (yet?) arrays in the parameter list (ILogEventEnricher[])
319
+ internal static LoggerConfiguration With ( LoggerFilterConfiguration loggerFilterConfiguration , ILogEventFilter filter )
320
+ {
321
+ return loggerFilterConfiguration . With ( filter ) ;
322
+ }
323
+
324
+ // Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
325
+ internal static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
326
+ {
327
+ return loggerEnrichmentConfiguration . FromLogContext ( ) ;
328
+ }
329
+
330
+ internal static MethodInfo GetSurrogateConfigurationMethod < TConfiguration , TArg > ( Expression < Action < TConfiguration , TArg > > method )
331
+ {
332
+ return ( method . Body as MethodCallExpression ) ? . Method ;
333
+ }
306
334
}
307
335
}
0 commit comments