1111using Serilog . Core ;
1212using Serilog . Debugging ;
1313using Serilog . Events ;
14- using System . Linq . Expressions ;
1514using System . Text . RegularExpressions ;
1615
1716namespace Serilog . Settings . Configuration
@@ -155,10 +154,10 @@ void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<s
155154
156155 void ApplyDestructuring ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
157156 {
158- var filterDirective = _section . GetSection ( "Destructure" ) ;
159- if ( filterDirective . GetChildren ( ) . Any ( ) )
157+ var destructureDirective = _section . GetSection ( "Destructure" ) ;
158+ if ( destructureDirective . GetChildren ( ) . Any ( ) )
160159 {
161- var methodCalls = GetMethodCalls ( filterDirective ) ;
160+ var methodCalls = GetMethodCalls ( destructureDirective ) ;
162161 CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure , declaredLevelSwitches ) ;
163162 }
164163 }
@@ -221,9 +220,11 @@ internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMet
221220 where child . Value == null
222221 let name = GetSectionName ( child )
223222 let callArgs = ( from argument in child . GetSection ( "Args" ) . GetChildren ( )
224- select new {
223+ select new
224+ {
225225 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 )
227228 select new { Name = name , Args = callArgs } ) )
228229 . ToLookup ( p => p . Name , p => p . Args ) ;
229230
@@ -330,7 +331,7 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
330331 select directive . Key == null ? p . DefaultValue : directive . Value . ConvertTo ( p . ParameterType , declaredLevelSwitches ) ) . ToList ( ) ;
331332
332333 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 ;
334335
335336 call . Insert ( 0 , receiver ) ;
336337
@@ -352,7 +353,7 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IReadOnlyCollecti
352353 {
353354 var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
354355 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 ) ;
356357
357358 return found ;
358359 }
@@ -368,21 +369,16 @@ internal static IList<MethodInfo> FindFilterConfigurationMethods(IReadOnlyCollec
368369 {
369370 var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerFilterConfiguration ) ) ;
370371 if ( configurationAssemblies . Contains ( typeof ( LoggerFilterConfiguration ) . GetTypeInfo ( ) . Assembly ) )
371- found . Add ( GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter , object > ( ( c , f , _ ) => With ( c , f ) ) ) ;
372+ found . AddRange ( SurrogateConfigurationMethods . Filter ) ;
372373
373374 return found ;
374375 }
375376
376377 internal static IList < MethodInfo > FindDestructureConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
377378 {
378379 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 ) ;
386382
387383 return found ;
388384 }
@@ -391,12 +387,12 @@ internal static IList<MethodInfo> FindEventEnricherConfigurationMethods(IReadOnl
391387 {
392388 var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
393389 if ( configurationAssemblies . Contains ( typeof ( LoggerEnrichmentConfiguration ) . GetTypeInfo ( ) . Assembly ) )
394- found . Add ( GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object , object > ( ( c , _ , __ ) => FromLogContext ( c ) ) ) ;
390+ found . AddRange ( SurrogateConfigurationMethods . Enrich ) ;
395391
396392 return found ;
397393 }
398394
399- internal static IList < MethodInfo > FindConfigurationExtensionMethods ( IReadOnlyCollection < Assembly > configurationAssemblies , Type configType )
395+ internal static List < MethodInfo > FindConfigurationExtensionMethods ( IReadOnlyCollection < Assembly > configurationAssemblies , Type configType )
400396 {
401397 return configurationAssemblies
402398 . SelectMany ( a => a . ExportedTypes
@@ -408,45 +404,6 @@ internal static IList<MethodInfo> FindConfigurationExtensionMethods(IReadOnlyCol
408404 . ToList ( ) ;
409405 }
410406
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-
450407 internal static bool IsValidSwitchName ( string input )
451408 {
452409 return Regex . IsMatch ( input , LevelSwitchNameRegex ) ;
0 commit comments