Skip to content

Commit

Permalink
Merge pull request #128 from MV10/issue_111
Browse files Browse the repository at this point in the history
fixes #111 - case-insensitive method argument-matching
  • Loading branch information
MV10 committed Sep 16, 2018
2 parents a6177b7 + 0095855 commit 1ee618a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu
if (methodInfo != null)
{
var call = (from p in methodInfo.GetParameters().Skip(1)
let directive = method.Value.FirstOrDefault(s => s.Key == p.Name)
let directive = method.Value.FirstOrDefault(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))
select directive.Key == null ? p.DefaultValue : directive.Value.ConvertTo(p.ParameterType, declaredLevelSwitches)).ToList();

var parm = methodInfo.GetParameters().FirstOrDefault(i => i.ParameterType == typeof(IConfiguration));
Expand All @@ -342,10 +342,15 @@ static void CallConfigurationMethods(ILookup<string, Dictionary<string, IConfigu

internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> candidateMethods, string name, Dictionary<string, IConfigurationArgumentValue> suppliedArgumentValues)
{
// Per issue #111, it is safe to use case-insensitive matching on argument names. The CLR doesn't permit this type
// of overloading, and the Microsoft.Extensions.Configuration keys are case-insensitive (case is preserved with some
// config sources, but key-matching is case-insensitive and case-preservation does not appear to be guaranteed).
return candidateMethods
.Where(m => m.Name == name &&
m.GetParameters().Skip(1).All(p => p.HasDefaultValue || suppliedArgumentValues.Any(s => s.Key == p.Name)))
.OrderByDescending(m => m.GetParameters().Count(p => suppliedArgumentValues.Any(s => s.Key == p.Name)))
m.GetParameters().Skip(1)
.All(p => p.HasDefaultValue || suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))))
.OrderByDescending(m =>
m.GetParameters().Count(p => suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))))
.FirstOrDefault();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,30 @@ public void SinkWithIntArrayArgument()
Assert.Equal(1, DummyRollingFileSink.Emitted.Count);
}

[Trait("Bugfix", "#111")]
[Fact]
public void CaseInsensitiveArgumentNameMatching()
{
var json = @"{
""Serilog"": {
""Using"": [""TestDummies""],
""WriteTo"": [{
""Name"": ""DummyRollingFile"",
""Args"": {""PATHFORMAT"" : ""C:\\""}
}]
}
}";

var log = ConfigFromJson(json)
.CreateLogger();

DummyRollingFileSink.Emitted.Clear();

log.Write(Some.InformationEvent());

Assert.Equal(1, DummyRollingFileSink.Emitted.Count);
}

[Trait("Bugfix", "#91")]
[Fact]
public void WriteToLoggerWithRestrictedToMinimumLevelIsSupported()
Expand Down

0 comments on commit 1ee618a

Please sign in to comment.