1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq . Expressions ;
3
+ using System . Linq ;
4
4
using System . Reflection ;
5
5
using Serilog . Configuration ;
6
6
using Serilog . Core ;
@@ -10,66 +10,73 @@ namespace Serilog.Settings.Configuration
10
10
{
11
11
/// <summary>
12
12
/// 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
14
14
/// are actually "regular" method calls and would not be found otherwise.
15
15
///
16
16
/// This static class contains internal methods that can be used instead.
17
17
///
18
18
/// </summary>
19
19
static class SurrogateConfigurationMethods
20
20
{
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 ) ] ;
59
32
60
33
/*
61
34
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.
66
38
*/
67
39
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
+ // =======
68
71
// TODO: add overload for array argument (ILogEventEnricher[])
72
+ // expose `With(params ILogEventFilter[] filters)` as if it was `With(ILogEventFilter filter)`
69
73
static LoggerConfiguration With ( LoggerFilterConfiguration loggerFilterConfiguration , ILogEventFilter filter )
70
74
=> loggerFilterConfiguration . With ( filter ) ;
71
75
76
+ // .Destructure...
77
+ // ============
72
78
// TODO: add overload for array argument (IDestructuringPolicy[])
79
+ // expose `With(params IDestructuringPolicy[] destructuringPolicies)` as if it was `With(IDestructuringPolicy policy)`
73
80
static LoggerConfiguration With ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , IDestructuringPolicy policy )
74
81
=> loggerDestructuringConfiguration . With ( policy ) ;
75
82
@@ -85,15 +92,17 @@ static LoggerConfiguration ToMaximumCollectionCount(LoggerDestructuringConfigura
85
92
static LoggerConfiguration AsScalar ( LoggerDestructuringConfiguration loggerDestructuringConfiguration , Type scalarType )
86
93
=> loggerDestructuringConfiguration . AsScalar ( scalarType ) ;
87
94
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
+
88
103
static LoggerConfiguration FromLogContext ( LoggerEnrichmentConfiguration loggerEnrichmentConfiguration )
89
104
=> loggerEnrichmentConfiguration . FromLogContext ( ) ;
90
105
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
98
107
}
99
108
}
0 commit comments