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" ) ;
@@ -156,31 +168,33 @@ Assembly[] LoadConfigurationAssemblies()
156
168
157
169
foreach ( var assemblyName in GetSerilogConfigurationAssemblies ( ) )
158
170
{
159
- var assumedName = new AssemblyName ( assemblyName ) ;
160
- var assumed = Assembly . Load ( assumedName ) ;
171
+ var assumed = Assembly . Load ( assemblyName ) ;
161
172
if ( assumed != null && ! assemblies . ContainsKey ( assumed . FullName ) )
162
173
assemblies . Add ( assumed . FullName , assumed ) ;
163
174
}
164
175
165
176
return assemblies . Values . ToArray ( ) ;
166
177
}
167
178
168
- string [ ] GetSerilogConfigurationAssemblies ( )
179
+ AssemblyName [ ] GetSerilogConfigurationAssemblies ( )
169
180
{
170
- var query = Enumerable . Empty < string > ( ) ;
181
+ var query = Enumerable . Empty < AssemblyName > ( ) ;
171
182
var filter = new Func < string , bool > ( name => name != null && name . ToLowerInvariant ( ) . Contains ( "serilog" ) ) ;
172
183
173
184
if ( _dependencyContext != null )
174
185
{
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 ;
176
190
}
177
191
else
178
192
{
179
193
#if APPDOMAIN
180
194
query = from outputAssemblyPath in System . IO . Directory . GetFiles ( AppDomain . CurrentDomain . BaseDirectory , "*.dll" )
181
195
let assemblyFileName = System . IO . Path . GetFileNameWithoutExtension ( outputAssemblyPath )
182
196
where filter ( assemblyFileName )
183
- select AssemblyName . GetAssemblyName ( outputAssemblyPath ) . FullName ;
197
+ select AssemblyName . GetAssemblyName ( outputAssemblyPath ) ;
184
198
#endif
185
199
}
186
200
@@ -273,21 +287,20 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IEnumerable<Assem
273
287
return FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
274
288
}
275
289
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 )
278
291
{
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 ) ) ) ;
281
295
282
- static readonly MethodInfo SurrogateFromLogContextConfigurationMethod = typeof ( ConfigurationReader )
283
- . GetTypeInfo ( )
284
- . DeclaredMethods . Single ( m => m . Name == "FromLogContext" ) ;
296
+ return found ;
297
+ }
285
298
286
299
internal static IList < MethodInfo > FindEventEnricherConfigurationMethods ( IEnumerable < Assembly > configurationAssemblies )
287
300
{
288
301
var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
289
302
if ( configurationAssemblies . Contains ( typeof ( LoggerEnrichmentConfiguration ) . GetTypeInfo ( ) . Assembly ) )
290
- found . Add ( SurrogateFromLogContextConfigurationMethod ) ;
303
+ found . Add ( GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object > ( ( c , _ ) => FromLogContext ( c ) ) ) ;
291
304
292
305
return found ;
293
306
}
@@ -303,5 +316,22 @@ internal static IList<MethodInfo> FindConfigurationMethods(IEnumerable<Assembly>
303
316
. Where ( m => m . GetParameters ( ) [ 0 ] . ParameterType == configType )
304
317
. ToList ( ) ;
305
318
}
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
+ }
306
336
}
307
337
}
0 commit comments