Skip to content

Commit 565af1a

Browse files
author
Jan Zahradník
committed
Exceptions for misconfiguration
1 parent 71ee59d commit 565af1a

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
353353
// Per issue #111, it is safe to use case-insensitive matching on argument names. The CLR doesn't permit this type
354354
// of overloading, and the Microsoft.Extensions.Configuration keys are case-insensitive (case is preserved with some
355355
// config sources, but key-matching is case-insensitive and case-preservation does not appear to be guaranteed).
356-
return candidateMethods
356+
var selectedMethod = candidateMethods
357357
.Where(m => m.Name == name &&
358358
m.GetParameters().Skip(1)
359359
.All(p => p.HasDefaultValue
@@ -372,6 +372,25 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
372372
matchingArgs.Count(p => p.ParameterType == typeof(string)));
373373
})
374374
.FirstOrDefault();
375+
376+
if (selectedMethod == null)
377+
{
378+
var methodsByName = candidateMethods.Where(m => m.Name == name).ToList();
379+
if (!methodsByName.Any())
380+
throw new MissingMethodException($"Unable to find a method called {name}. Candidate methods are:{Environment.NewLine}{string.Join(Environment.NewLine, candidateMethods)}");
381+
382+
string msg = $"Unable to find a method called {name}"
383+
+ (suppliedArgumentValues.Any()
384+
? "for supplied arguments: " + string.Join(", ", suppliedArgumentValues.Keys)
385+
: "with no supplied arguments")
386+
+ ". Candidate methods are:"
387+
+ Environment.NewLine
388+
+ string.Join(Environment.NewLine, methodsByName);
389+
390+
throw new MissingMethodException(msg);
391+
}
392+
393+
return selectedMethod;
375394
}
376395

377396
static IList<MethodInfo> FindSinkConfigurationMethods(IReadOnlyCollection<Assembly> configurationAssemblies)

0 commit comments

Comments
 (0)