11
11
using Serilog . Core ;
12
12
using Serilog . Debugging ;
13
13
using Serilog . Events ;
14
- using System . Linq . Expressions ;
15
14
using System . Text . RegularExpressions ;
16
15
17
16
namespace Serilog . Settings . Configuration
@@ -155,10 +154,10 @@ void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<s
155
154
156
155
void ApplyDestructuring ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
157
156
{
158
- var filterDirective = _section . GetSection ( "Destructure" ) ;
159
- if ( filterDirective . GetChildren ( ) . Any ( ) )
157
+ var destructureDirective = _section . GetSection ( "Destructure" ) ;
158
+ if ( destructureDirective . GetChildren ( ) . Any ( ) )
160
159
{
161
- var methodCalls = GetMethodCalls ( filterDirective ) ;
160
+ var methodCalls = GetMethodCalls ( destructureDirective ) ;
162
161
CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure , declaredLevelSwitches ) ;
163
162
}
164
163
}
@@ -221,9 +220,11 @@ internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMet
221
220
where child . Value == null
222
221
let name = GetSectionName ( child )
223
222
let callArgs = ( from argument in child . GetSection ( "Args" ) . GetChildren ( )
224
- select new {
223
+ select new
224
+ {
225
225
Name = argument . Key ,
226
- Value = GetArgumentValue ( argument ) } ) . ToDictionary ( p => p . Name , p => p . Value )
226
+ Value = GetArgumentValue ( argument )
227
+ } ) . ToDictionary ( p => p . Name , p => p . Value )
227
228
select new { Name = name , Args = callArgs } ) )
228
229
. ToLookup ( p => p . Name , p => p . Args ) ;
229
230
@@ -330,7 +331,7 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
330
331
select directive . Key == null ? p . DefaultValue : directive . Value . ConvertTo ( p . ParameterType , declaredLevelSwitches ) ) . ToList ( ) ;
331
332
332
333
var parm = methodInfo . GetParameters ( ) . FirstOrDefault ( i => i . ParameterType == typeof ( IConfiguration ) ) ;
333
- if ( parm != null ) call [ parm . Position - 1 ] = _configuration ;
334
+ if ( parm != null ) call [ parm . Position - 1 ] = _configuration ;
334
335
335
336
call . Insert ( 0 , receiver ) ;
336
337
@@ -352,7 +353,7 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IReadOnlyCollecti
352
353
{
353
354
var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
354
355
if ( configurationAssemblies . Contains ( typeof ( LoggerSinkConfiguration ) . GetTypeInfo ( ) . Assembly ) )
355
- found . Add ( GetSurrogateConfigurationMethod < LoggerSinkConfiguration , Action < LoggerConfiguration > , LoggingLevelSwitch > ( ( c , a , s ) => Logger ( c , a , LevelAlias . Minimum , s ) ) ) ;
356
+ found . AddRange ( SurrogateConfigurationMethods . WriteTo ) ;
356
357
357
358
return found ;
358
359
}
@@ -368,21 +369,16 @@ internal static IList<MethodInfo> FindFilterConfigurationMethods(IReadOnlyCollec
368
369
{
369
370
var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerFilterConfiguration ) ) ;
370
371
if ( configurationAssemblies . Contains ( typeof ( LoggerFilterConfiguration ) . GetTypeInfo ( ) . Assembly ) )
371
- found . Add ( GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter , object > ( ( c , f , _ ) => With ( c , f ) ) ) ;
372
+ found . AddRange ( SurrogateConfigurationMethods . Filter ) ;
372
373
373
374
return found ;
374
375
}
375
376
376
377
internal static IList < MethodInfo > FindDestructureConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
377
378
{
378
379
var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerDestructuringConfiguration ) ) ;
379
- if ( configurationAssemblies . Contains ( typeof ( LoggerDestructuringConfiguration ) . GetTypeInfo ( ) . Assembly ) )
380
- {
381
- found . Add ( GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , IDestructuringPolicy , object > ( ( c , d , _ ) => With ( c , d ) ) ) ;
382
- found . Add ( GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumDepth ( c , m ) ) ) ;
383
- found . Add ( GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumStringLength ( c , m ) ) ) ;
384
- found . Add ( GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumCollectionCount ( c , m ) ) ) ;
385
- }
380
+ if ( configurationAssemblies . Contains ( typeof ( LoggerDestructuringConfiguration ) . GetTypeInfo ( ) . Assembly ) )
381
+ found . AddRange ( SurrogateConfigurationMethods . Destructure ) ;
386
382
387
383
return found ;
388
384
}
@@ -391,12 +387,12 @@ internal static IList<MethodInfo> FindEventEnricherConfigurationMethods(IReadOnl
391
387
{
392
388
var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
393
389
if ( configurationAssemblies . Contains ( typeof ( LoggerEnrichmentConfiguration ) . GetTypeInfo ( ) . Assembly ) )
394
- found . Add ( GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object , object > ( ( c , _ , __ ) => FromLogContext ( c ) ) ) ;
390
+ found . AddRange ( SurrogateConfigurationMethods . Enrich ) ;
395
391
396
392
return found ;
397
393
}
398
394
399
- internal static IList < MethodInfo > FindConfigurationExtensionMethods ( IReadOnlyCollection < Assembly > configurationAssemblies , Type configType )
395
+ internal static List < MethodInfo > FindConfigurationExtensionMethods ( IReadOnlyCollection < Assembly > configurationAssemblies , Type configType )
400
396
{
401
397
return configurationAssemblies
402
398
. SelectMany ( a => a . ExportedTypes
@@ -408,45 +404,6 @@ internal static IList<MethodInfo> FindConfigurationExtensionMethods(IReadOnlyCol
408
404
. ToList ( ) ;
409
405
}
410
406
411
- /*
412
- Pass-through calls to various Serilog config methods which are
413
- implemented as instance methods rather than extension methods. The
414
- FindXXXConfigurationMethods calls (above) use these to add method
415
- invocation expressions as surrogates so that SelectConfigurationMethod
416
- has a way to match and invoke these instance methods.
417
- */
418
-
419
- // TODO: add overload for array argument (ILogEventEnricher[])
420
- internal static LoggerConfiguration With ( LoggerFilterConfiguration loggerFilterConfiguration , ILogEventFilter filter )
421
- => loggerFilterConfiguration . With ( filter ) ;
422
-
423
- // TODO: add overload for array argument (IDestructuringPolicy[])
424
- internal static LoggerConfiguration With ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , IDestructuringPolicy policy )
425
- => loggerDestructuringConfiguration . With ( policy ) ;
426
-
427
- internal static LoggerConfiguration ToMaximumDepth ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , int maximumDestructuringDepth )
428
- => loggerDestructuringConfiguration . ToMaximumDepth ( maximumDestructuringDepth ) ;
429
-
430
- internal static LoggerConfiguration ToMaximumStringLength ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , int maximumStringLength )
431
- => loggerDestructuringConfiguration . ToMaximumStringLength ( maximumStringLength ) ;
432
-
433
- internal static LoggerConfiguration ToMaximumCollectionCount ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , int maximumCollectionCount )
434
- => loggerDestructuringConfiguration . ToMaximumCollectionCount ( maximumCollectionCount ) ;
435
-
436
- internal static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
437
- => loggerEnrichmentConfiguration . FromLogContext ( ) ;
438
-
439
- // Unlike the other configuration methods, Logger is an instance method rather than an extension.
440
- internal static LoggerConfiguration Logger (
441
- LoggerSinkConfiguration loggerSinkConfiguration ,
442
- Action < LoggerConfiguration > configureLogger ,
443
- LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
444
- LoggingLevelSwitch levelSwitch = null )
445
- => loggerSinkConfiguration . Logger ( configureLogger , restrictedToMinimumLevel , levelSwitch ) ;
446
-
447
- internal static MethodInfo GetSurrogateConfigurationMethod < TConfiguration , TArg1 , TArg2 > ( Expression < Action < TConfiguration , TArg1 , TArg2 > > method )
448
- => ( method . Body as MethodCallExpression ) ? . Method ;
449
-
450
407
internal static bool IsValidSwitchName ( string input )
451
408
{
452
409
return Regex . IsMatch ( input , LevelSwitchNameRegex ) ;
0 commit comments