Skip to content

Commit fbe4f2f

Browse files
authored
Merge pull request #132 from nbaztec/fix-issue/131
fixes #131 by preferring string overloaded methods
2 parents d6118bf + 578cfc1 commit fbe4f2f

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ bld/
2525
# Visual Studo 2015 cache/options directory
2626
.vs/
2727

28+
# JetBrains project files
29+
.idea/
30+
2831
# MSTest test Results
2932
[Tt]est[Rr]esult*/
3033
[Bb]uild[Ll]og.*

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,15 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
350350
m.GetParameters().Skip(1)
351351
.All(p => p.HasDefaultValue || suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))))
352352
.OrderByDescending(m =>
353-
m.GetParameters().Count(p => suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))))
353+
{
354+
var matchingArgs = m.GetParameters().Where(p => suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))).ToList();
355+
356+
// Prefer the configuration method with most number of matching arguments and of those the ones with
357+
// the most string type parameters to predict best match with least type casting
358+
return new Tuple<int, int>(
359+
matchingArgs.Count,
360+
matchingArgs.Count(p => p.ParameterType == typeof(string)));
361+
})
354362
.FirstOrDefault();
355363
}
356364

test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,20 @@ public void MethodsAreSelectedBasedOnCountOfMatchedArguments()
165165
var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArguments);
166166
Assert.Equal(typeof(ITextFormatter), selected.GetParameters()[1].ParameterType);
167167
}
168+
169+
[Fact]
170+
public void MethodsAreSelectedBasedOnCountOfMatchedArgumentsAndThenStringType()
171+
{
172+
var options = typeof(DummyLoggerConfigurationWithMultipleMethodsExtensions).GetTypeInfo().DeclaredMethods.ToList();
173+
Assert.Equal(3, options.Count(mi => mi.Name == "DummyRollingFile"));
174+
var suppliedArguments = new Dictionary<string, IConfigurationArgumentValue>()
175+
{
176+
{ "pathFormat", new StringArgumentValue(() => "C:\\") },
177+
{ "formatter", new StringArgumentValue(() => "SomeFormatter, SomeAssembly") }
178+
};
179+
180+
var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArguments);
181+
Assert.Equal(typeof(string), selected.GetParameters()[2].ParameterType);
182+
}
168183
}
169184
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using Serilog.Configuration;
3+
using Serilog.Events;
4+
using Serilog.Formatting;
5+
6+
namespace Serilog.Settings.Configuration.Tests
7+
{
8+
using System.Collections.Generic;
9+
10+
static class DummyLoggerConfigurationWithMultipleMethodsExtensions
11+
{
12+
public static LoggerConfiguration DummyRollingFile(
13+
LoggerSinkConfiguration loggerSinkConfiguration,
14+
ITextFormatter formatter,
15+
IEnumerable<string> pathFormat,
16+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
17+
string outputTemplate = null,
18+
IFormatProvider formatProvider = null)
19+
{
20+
return null;
21+
}
22+
23+
public static LoggerConfiguration DummyRollingFile(
24+
LoggerSinkConfiguration loggerSinkConfiguration,
25+
ITextFormatter formatter,
26+
string pathFormat,
27+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
28+
string outputTemplate = null,
29+
IFormatProvider formatProvider = null)
30+
{
31+
return null;
32+
}
33+
34+
public static LoggerConfiguration DummyRollingFile(
35+
LoggerSinkConfiguration loggerSinkConfiguration,
36+
string pathFormat,
37+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
38+
{
39+
return null;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)