1111using Serilog . Core ;
1212using Serilog . Debugging ;
1313using Serilog . Events ;
14+ using System . Linq . Expressions ;
1415
1516namespace Serilog . Settings . Configuration
1617{
@@ -33,9 +34,20 @@ public void Configure(LoggerConfiguration loggerConfiguration)
3334
3435 ApplyMinimumLevel ( loggerConfiguration ) ;
3536 ApplyEnrichment ( loggerConfiguration , configurationAssemblies ) ;
37+ ApplyFilters ( loggerConfiguration , configurationAssemblies ) ;
3638 ApplySinks ( loggerConfiguration , configurationAssemblies ) ;
3739 }
3840
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+
3951 void ApplySinks ( LoggerConfiguration loggerConfiguration , Assembly [ ] configurationAssemblies )
4052 {
4153 var writeToDirective = _configuration . GetSection ( "WriteTo" ) ;
@@ -156,31 +168,33 @@ Assembly[] LoadConfigurationAssemblies()
156168
157169 foreach ( var assemblyName in GetSerilogConfigurationAssemblies ( ) )
158170 {
159- var assumedName = new AssemblyName ( assemblyName ) ;
160- var assumed = Assembly . Load ( assumedName ) ;
171+ var assumed = Assembly . Load ( assemblyName ) ;
161172 if ( assumed != null && ! assemblies . ContainsKey ( assumed . FullName ) )
162173 assemblies . Add ( assumed . FullName , assumed ) ;
163174 }
164175
165176 return assemblies . Values . ToArray ( ) ;
166177 }
167178
168- string [ ] GetSerilogConfigurationAssemblies ( )
179+ AssemblyName [ ] GetSerilogConfigurationAssemblies ( )
169180 {
170- var query = Enumerable . Empty < string > ( ) ;
181+ var query = Enumerable . Empty < AssemblyName > ( ) ;
171182 var filter = new Func < string , bool > ( name => name != null && name . ToLowerInvariant ( ) . Contains ( "serilog" ) ) ;
172183
173184 if ( _dependencyContext != null )
174185 {
175- query = from lib in _dependencyContext . RuntimeLibraries where filter ( lib . Name ) select lib . Name ;
186+ query = from library in _dependencyContext . RuntimeLibraries
187+ from assemblyName in library . GetDefaultAssemblyNames ( _dependencyContext )
188+ where filter ( assemblyName . Name )
189+ select assemblyName ;
176190 }
177191 else
178192 {
179193#if APPDOMAIN
180194 query = from outputAssemblyPath in System . IO . Directory . GetFiles ( AppDomain . CurrentDomain . BaseDirectory , "*.dll" )
181195 let assemblyFileName = System . IO . Path . GetFileNameWithoutExtension ( outputAssemblyPath )
182196 where filter ( assemblyFileName )
183- select AssemblyName . GetAssemblyName ( outputAssemblyPath ) . FullName ;
197+ select AssemblyName . GetAssemblyName ( outputAssemblyPath ) ;
184198#endif
185199 }
186200
@@ -273,21 +287,20 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IEnumerable<Assem
273287 return FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
274288 }
275289
276- // Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
277- internal static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
290+ internal static IList < MethodInfo > FindFilterConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
278291 {
279- return loggerEnrichmentConfiguration . FromLogContext ( ) ;
280- }
292+ var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerFilterConfiguration ) ) ;
293+ if ( configurationAssemblies . Contains ( typeof ( LoggerFilterConfiguration ) . GetTypeInfo ( ) . Assembly ) )
294+ found . Add ( GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter > ( ( c , f ) => With ( c , f ) ) ) ;
281295
282- static readonly MethodInfo SurrogateFromLogContextConfigurationMethod = typeof ( ConfigurationReader )
283- . GetTypeInfo ( )
284- . DeclaredMethods . Single ( m => m . Name == "FromLogContext" ) ;
296+ return found ;
297+ }
285298
286299 internal static IList < MethodInfo > FindEventEnricherConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
287300 {
288301 var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
289302 if ( configurationAssemblies . Contains ( typeof ( LoggerEnrichmentConfiguration ) . GetTypeInfo ( ) . Assembly ) )
290- found . Add ( SurrogateFromLogContextConfigurationMethod ) ;
303+ found . Add ( GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object > ( ( c , _ ) => FromLogContext ( c ) ) ) ;
291304
292305 return found ;
293306 }
@@ -303,5 +316,22 @@ internal static IList<MethodInfo> FindConfigurationMethods(IEnumerable<Assembly>
303316 . Where ( m => m . GetParameters ( ) [ 0 ] . ParameterType == configType )
304317 . ToList ( ) ;
305318 }
319+
320+ // don't support (yet?) arrays in the parameter list (ILogEventEnricher[])
321+ internal static LoggerConfiguration With ( LoggerFilterConfiguration loggerFilterConfiguration , ILogEventFilter filter )
322+ {
323+ return loggerFilterConfiguration . With ( filter ) ;
324+ }
325+
326+ // Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
327+ internal static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
328+ {
329+ return loggerEnrichmentConfiguration . FromLogContext ( ) ;
330+ }
331+
332+ internal static MethodInfo GetSurrogateConfigurationMethod < TConfiguration , TArg > ( Expression < Action < TConfiguration , TArg > > method )
333+ {
334+ return ( method . Body as MethodCallExpression ) ? . Method ;
335+ }
306336 }
307337}
0 commit comments