Skip to content

Commit a55fc9f

Browse files
committed
Refactor SurrogateConfigurationMethods
so that all methods in the class are dynamically discovered and exposed to Serilog settings as if they were regular extension methods
1 parent 70e4f0b commit a55fc9f

File tree

1 file changed

+16
-48
lines changed

1 file changed

+16
-48
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/SurrogateConfigurationMethods.cs

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq.Expressions;
3+
using System.Linq;
44
using System.Reflection;
55
using Serilog.Configuration;
66
using Serilog.Core;
@@ -10,62 +10,25 @@ 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-
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());
4525

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-
}
5726

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)];
6932

7033
/*
7134
Pass-through calls to various Serilog config methods which are
@@ -75,6 +38,10 @@ invocation expressions as surrogates so that SelectConfigurationMethod
7538
has a way to match and invoke these instance methods.
7639
*/
7740

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+
7845
// .WriteTo...
7946
// ========
8047
static LoggerConfiguration Sink(
@@ -138,5 +105,6 @@ static LoggerConfiguration With(LoggerEnrichmentConfiguration loggerEnrichmentCo
138105
return loggerEnrichmentConfiguration.With(enricher);
139106
}
140107

108+
// ReSharper restore UnusedMember.Local
141109
}
142110
}

0 commit comments

Comments
 (0)