@@ -57,6 +57,7 @@ public void Configure(LoggerConfiguration loggerConfiguration)
57
57
ApplyMinimumLevel ( loggerConfiguration , declaredLevelSwitches ) ;
58
58
ApplyEnrichment ( loggerConfiguration , declaredLevelSwitches ) ;
59
59
ApplyFilters ( loggerConfiguration , declaredLevelSwitches ) ;
60
+ ApplyDestructuring ( loggerConfiguration , declaredLevelSwitches ) ;
60
61
ApplySinks ( loggerConfiguration , declaredLevelSwitches ) ;
61
62
ApplyAuditSinks ( loggerConfiguration , declaredLevelSwitches ) ;
62
63
}
@@ -152,6 +153,16 @@ void ApplyFilters(LoggerConfiguration loggerConfiguration, IReadOnlyDictionary<s
152
153
}
153
154
}
154
155
156
+ void ApplyDestructuring ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
157
+ {
158
+ var filterDirective = _section . GetSection ( "Destructure" ) ;
159
+ if ( filterDirective . GetChildren ( ) . Any ( ) )
160
+ {
161
+ var methodCalls = GetMethodCalls ( filterDirective ) ;
162
+ CallConfigurationMethods ( methodCalls , FindDestructureConfigurationMethods ( _configurationAssemblies ) , loggerConfiguration . Destructure , declaredLevelSwitches ) ;
163
+ }
164
+ }
165
+
155
166
void ApplySinks ( LoggerConfiguration loggerConfiguration , IReadOnlyDictionary < string , LoggingLevelSwitch > declaredLevelSwitches )
156
167
{
157
168
var writeToDirective = _section . GetSection ( "WriteTo" ) ;
@@ -339,7 +350,7 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
339
350
340
351
internal static IList < MethodInfo > FindSinkConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
341
352
{
342
- var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
353
+ var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerSinkConfiguration ) ) ;
343
354
if ( configurationAssemblies . Contains ( typeof ( LoggerSinkConfiguration ) . GetTypeInfo ( ) . Assembly ) )
344
355
found . Add ( GetSurrogateConfigurationMethod < LoggerSinkConfiguration , Action < LoggerConfiguration > , LoggingLevelSwitch > ( ( c , a , s ) => Logger ( c , a , LevelAlias . Minimum , s ) ) ) ;
345
356
@@ -348,30 +359,44 @@ internal static IList<MethodInfo> FindSinkConfigurationMethods(IReadOnlyCollecti
348
359
349
360
internal static IList < MethodInfo > FindAuditSinkConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
350
361
{
351
- var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerAuditSinkConfiguration ) ) ;
362
+ var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerAuditSinkConfiguration ) ) ;
352
363
353
364
return found ;
354
365
}
355
366
356
367
internal static IList < MethodInfo > FindFilterConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
357
368
{
358
- var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerFilterConfiguration ) ) ;
369
+ var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerFilterConfiguration ) ) ;
359
370
if ( configurationAssemblies . Contains ( typeof ( LoggerFilterConfiguration ) . GetTypeInfo ( ) . Assembly ) )
360
371
found . Add ( GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter , object > ( ( c , f , _ ) => With ( c , f ) ) ) ;
361
372
362
373
return found ;
363
374
}
364
375
376
+ internal static IList < MethodInfo > FindDestructureConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
377
+ {
378
+ 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
+ }
386
+
387
+ return found ;
388
+ }
389
+
365
390
internal static IList < MethodInfo > FindEventEnricherConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies )
366
391
{
367
- var found = FindConfigurationMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
392
+ var found = FindConfigurationExtensionMethods ( configurationAssemblies , typeof ( LoggerEnrichmentConfiguration ) ) ;
368
393
if ( configurationAssemblies . Contains ( typeof ( LoggerEnrichmentConfiguration ) . GetTypeInfo ( ) . Assembly ) )
369
394
found . Add ( GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object , object > ( ( c , _ , __ ) => FromLogContext ( c ) ) ) ;
370
395
371
396
return found ;
372
397
}
373
398
374
- internal static IList < MethodInfo > FindConfigurationMethods ( IReadOnlyCollection < Assembly > configurationAssemblies , Type configType )
399
+ internal static IList < MethodInfo > FindConfigurationExtensionMethods ( IReadOnlyCollection < Assembly > configurationAssemblies , Type configType )
375
400
{
376
401
return configurationAssemblies
377
402
. SelectMany ( a => a . ExportedTypes
@@ -383,15 +408,34 @@ internal static IList<MethodInfo> FindConfigurationMethods(IReadOnlyCollection<A
383
408
. ToList ( ) ;
384
409
}
385
410
386
- // don't support (yet?) arrays in the parameter list (ILogEventEnricher[])
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[])
387
420
internal static LoggerConfiguration With ( LoggerFilterConfiguration loggerFilterConfiguration , ILogEventFilter filter )
388
421
=> loggerFilterConfiguration . With ( filter ) ;
389
422
390
- // Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
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
+
391
436
internal static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
392
437
=> loggerEnrichmentConfiguration . FromLogContext ( ) ;
393
438
394
- // Unlike the other configuration methods, Logger is an instance method rather than an extension.
395
439
internal static LoggerConfiguration Logger (
396
440
LoggerSinkConfiguration loggerSinkConfiguration ,
397
441
Action < LoggerConfiguration > configureLogger ,
0 commit comments