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,62 +10,25 @@ 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
- yield return GetSurrogateConfigurationMethod < LoggerSinkConfiguration , ILogEventSink , LoggingLevelSwitch > ( ( c , sink , s ) => Sink ( c , sink , LevelAlias . Minimum , s ) ) ;
27
- }
28
- }
29
-
30
- public static IEnumerable < MethodInfo > AuditTo
31
- {
32
- get
33
- {
34
- yield return GetSurrogateConfigurationMethod < LoggerAuditSinkConfiguration , ILogEventSink , LoggingLevelSwitch > ( ( c , sink , s ) => Sink ( c , sink , LevelAlias . Minimum , s ) ) ;
35
- }
36
- }
37
-
38
- public static IEnumerable < MethodInfo > Filter
39
- {
40
- get
41
- {
42
- yield return GetSurrogateConfigurationMethod < LoggerFilterConfiguration , ILogEventFilter , object > ( ( c , f , _ ) => With ( c , f ) ) ;
43
- }
44
- }
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 ( ) ) ;
45
25
46
- public static IEnumerable < MethodInfo > Destructure
47
- {
48
- get
49
- {
50
- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , IDestructuringPolicy , object > ( ( c , d , _ ) => With ( c , d ) ) ;
51
- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumDepth ( c , m ) ) ;
52
- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumStringLength ( c , m ) ) ;
53
- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , int , object > ( ( c , m , _ ) => ToMaximumCollectionCount ( c , m ) ) ;
54
- yield return GetSurrogateConfigurationMethod < LoggerDestructuringConfiguration , Type , object > ( ( c , t , _ ) => AsScalar ( c , t ) ) ;
55
- }
56
- }
57
26
58
- public static IEnumerable < MethodInfo > Enrich
59
- {
60
- get
61
- {
62
- yield return GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , object , object > ( ( c , _ , __ ) => FromLogContext ( c ) ) ;
63
- yield return GetSurrogateConfigurationMethod < LoggerEnrichmentConfiguration , ILogEventEnricher , object > ( ( c , e , __ ) => With ( c , e ) ) ;
64
- }
65
- }
66
-
67
- static MethodInfo GetSurrogateConfigurationMethod < TConfiguration , TArg1 , TArg2 > ( Expression < Action < TConfiguration , TArg1 , TArg2 > > method )
68
- => ( method . Body as MethodCallExpression ) ? . Method ;
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 ) ] ;
69
32
70
33
/*
71
34
Pass-through calls to various Serilog config methods which are
@@ -75,6 +38,10 @@ invocation expressions as surrogates so that SelectConfigurationMethod
75
38
has a way to match and invoke these instance methods.
76
39
*/
77
40
41
+ // ReSharper disable UnusedMember.Local
42
+ // those methods are discovered through reflection by `SurrogateMethodCandidates`
43
+ // ReSharper has no way to see that they are actually used ...
44
+
78
45
// .WriteTo...
79
46
// ========
80
47
static LoggerConfiguration Sink (
@@ -138,5 +105,6 @@ static LoggerConfiguration With(LoggerEnrichmentConfiguration loggerEnrichmentCo
138
105
return loggerEnrichmentConfiguration . With ( enricher ) ;
139
106
}
140
107
108
+ // ReSharper restore UnusedMember.Local
141
109
}
142
110
}
0 commit comments