11using System ;
22using System . Collections . Generic ;
3- using System . Linq . Expressions ;
3+ using System . Linq ;
44using System . Reflection ;
55using Serilog . Configuration ;
66using Serilog . Core ;
@@ -10,66 +10,73 @@ namespace Serilog.Settings.Configuration
1010{
1111 /// <summary>
1212 /// Contains "fake extension" methods for the Serilog configuration API.
13- /// By default the settings knows how to find extension methods, but some configuration
13+ /// By default the settings know how to find extension methods, but some configuration
1414 /// are actually "regular" method calls and would not be found otherwise.
1515 ///
1616 /// This static class contains internal methods that can be used instead.
1717 ///
1818 /// </summary>
1919 static class SurrogateConfigurationMethods
2020 {
21- public static IEnumerable < MethodInfo > WriteTo
22- {
23- get
24- {
25- yield return GetSurrogateConfigurationMethod < LoggerSinkConfiguration , Action < LoggerConfiguration > , LoggingLevelSwitch > ( ( c , a , s ) => Logger ( c , a , LevelAlias . Minimum , s ) ) ;
26- }
27- }
28-
29- public static IEnumerable < MethodInfo > Filter
30- {
31- get
32- {
33- yield return GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter , object > ( ( c , f , _ ) => With ( c , f ) ) ;
34- }
35- }
36-
37- public static IEnumerable < MethodInfo > Destructure
38- {
39- get
40- {
41- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , IDestructuringPolicy , object > ( ( c , d , _ ) => With ( c , d ) ) ;
42- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumDepth ( c , m ) ) ;
43- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumStringLength ( c , m ) ) ;
44- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumCollectionCount ( c , m ) ) ;
45- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , Type , object > ( ( c , t , _ ) => AsScalar ( c , t ) ) ;
46- }
47- }
48-
49- public static IEnumerable < MethodInfo > Enrich
50- {
51- get
52- {
53- yield return GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object , object > ( ( c , _ , __ ) => FromLogContext ( c ) ) ;
54- }
55- }
56-
57- static MethodInfo GetSurrogateConfigurationMethod < TConfiguration , TArg1 , TArg2 > ( Expression < Action < TConfiguration , TArg1 , TArg2 > > method )
58- => ( method . Body as MethodCallExpression ) ? . Method ;
21+ static readonly Dictionary < Type , MethodInfo [ ] > SurrogateMethodCandidates = typeof ( SurrogateConfigurationMethods )
22+ . GetTypeInfo ( ) . DeclaredMethods
23+ . GroupBy ( m => m . GetParameters ( ) . First ( ) . ParameterType )
24+ . ToDictionary ( g => g . Key , g => g . ToArray ( ) ) ;
25+
26+
27+ internal static readonly MethodInfo [ ] WriteTo = SurrogateMethodCandidates [ typeof ( LoggerSinkConfiguration ) ] ;
28+ internal static readonly MethodInfo [ ] AuditTo = SurrogateMethodCandidates [ typeof ( LoggerAuditSinkConfiguration ) ] ;
29+ internal static readonly MethodInfo [ ] Enrich = SurrogateMethodCandidates [ typeof ( LoggerEnrichmentConfiguration ) ] ;
30+ internal static readonly MethodInfo [ ] Destructure = SurrogateMethodCandidates [ typeof ( LoggerDestructuringConfiguration ) ] ;
31+ internal static readonly MethodInfo [ ] Filter = SurrogateMethodCandidates [ typeof ( LoggerFilterConfiguration ) ] ;
5932
6033 /*
6134 Pass-through calls to various Serilog config methods which are
62- implemented as instance methods rather than extension methods. The
63- FindXXXConfigurationMethods calls (above) use these to add method
64- invocation expressions as surrogates so that SelectConfigurationMethod
65- has a way to match and invoke these instance methods.
35+ implemented as instance methods rather than extension methods.
36+ ConfigurationReader adds those to the already discovered extension methods
37+ so they can be invoked as well.
6638 */
6739
40+ // ReSharper disable UnusedMember.Local
41+ // those methods are discovered through reflection by `SurrogateMethodCandidates`
42+ // ReSharper has no way to see that they are actually used ...
43+
44+ // .WriteTo...
45+ // ========
46+ static LoggerConfiguration Sink (
47+ LoggerSinkConfiguration loggerSinkConfiguration ,
48+ ILogEventSink sink ,
49+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
50+ LoggingLevelSwitch levelSwitch = null )
51+ => loggerSinkConfiguration . Sink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
52+
53+ static LoggerConfiguration Logger (
54+ LoggerSinkConfiguration loggerSinkConfiguration ,
55+ Action < LoggerConfiguration > configureLogger ,
56+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
57+ LoggingLevelSwitch levelSwitch = null )
58+ => loggerSinkConfiguration . Logger ( configureLogger , restrictedToMinimumLevel , levelSwitch ) ;
59+
60+ // .AuditTo...
61+ // ========
62+ static LoggerConfiguration Sink (
63+ LoggerAuditSinkConfiguration auditSinkConfiguration ,
64+ ILogEventSink sink ,
65+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
66+ LoggingLevelSwitch levelSwitch = null )
67+ => auditSinkConfiguration . Sink ( sink , restrictedToMinimumLevel , levelSwitch ) ;
68+
69+ // .Filter...
70+ // =======
6871 // TODO: add overload for array argument (ILogEventEnricher[])
72+ // expose `With(params ILogEventFilter[] filters)` as if it was `With(ILogEventFilter filter)`
6973 static LoggerConfiguration With ( LoggerFilterConfiguration loggerFilterConfiguration , ILogEventFilter filter )
7074 => loggerFilterConfiguration . With ( filter ) ;
7175
76+ // .Destructure...
77+ // ============
7278 // TODO: add overload for array argument (IDestructuringPolicy[])
79+ // expose `With(params IDestructuringPolicy[] destructuringPolicies)` as if it was `With(IDestructuringPolicy policy)`
7380 static LoggerConfiguration With ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , IDestructuringPolicy policy )
7481 => loggerDestructuringConfiguration . With ( policy ) ;
7582
@@ -85,15 +92,17 @@ static LoggerConfiguration ToMaximumCollectionCount(LoggerDestructuringConfigura
8592 static LoggerConfiguration AsScalar ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , Type scalarType )
8693 => loggerDestructuringConfiguration . AsScalar ( scalarType ) ;
8794
95+ // .Enrich...
96+ // =======
97+ // expose `With(params ILogEventEnricher[] enrichers)` as if it was `With(ILogEventEnricher enricher)`
98+ static LoggerConfiguration With (
99+ LoggerEnrichmentConfiguration loggerEnrichmentConfiguration ,
100+ ILogEventEnricher enricher )
101+ => loggerEnrichmentConfiguration . With ( enricher ) ;
102+
88103 static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
89104 => loggerEnrichmentConfiguration . FromLogContext ( ) ;
90105
91- // Unlike the other configuration methods, Logger is an instance method rather than an extension.
92- static LoggerConfiguration Logger (
93- LoggerSinkConfiguration loggerSinkConfiguration ,
94- Action < LoggerConfiguration > configureLogger ,
95- LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
96- LoggingLevelSwitch levelSwitch = null )
97- => loggerSinkConfiguration . Logger ( configureLogger , restrictedToMinimumLevel , levelSwitch ) ;
106+ // ReSharper restore UnusedMember.Local
98107 }
99108}
0 commit comments