Skip to content

Commit 5a466f5

Browse files
committed
Improved SelfLog messages when misconfigured
2 parents 0e42736 + 82199f0 commit 5a466f5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
349349
// Per issue #111, it is safe to use case-insensitive matching on argument names. The CLR doesn't permit this type
350350
// of overloading, and the Microsoft.Extensions.Configuration keys are case-insensitive (case is preserved with some
351351
// config sources, but key-matching is case-insensitive and case-preservation does not appear to be guaranteed).
352-
return candidateMethods
352+
var selectedMethod = candidateMethods
353353
.Where(m => m.Name == name)
354354
.Where(m => m.GetParameters()
355355
.Skip(1)
@@ -366,6 +366,27 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
366366
matchingArgs.Count(p => p.ParameterType == typeof(string)));
367367
})
368368
.FirstOrDefault();
369+
370+
if (selectedMethod == null)
371+
{
372+
var methodsByName = candidateMethods
373+
.Where(m => m.Name == name)
374+
.Select(m => $"{m.Name}({string.Join(", ", m.GetParameters().Skip(1).Select(p => p.Name))})")
375+
.ToList();
376+
377+
if (!methodsByName.Any())
378+
SelfLog.WriteLine($"Unable to find a method called {name}. Candidate methods are:{Environment.NewLine}{string.Join(Environment.NewLine, candidateMethods)}");
379+
else
380+
SelfLog.WriteLine($"Unable to find a method called {name} "
381+
+ (suppliedArgumentNames.Any()
382+
? "for supplied arguments: " + string.Join(", ", suppliedArgumentNames)
383+
: "with no supplied arguments")
384+
+ ". Candidate methods are:"
385+
+ Environment.NewLine
386+
+ string.Join(Environment.NewLine, methodsByName));
387+
}
388+
389+
return selectedMethod;
369390
}
370391

371392
static bool ParameterNameMatches(string actualParameterName, string suppliedName)

0 commit comments

Comments
 (0)