Skip to content

Commit

Permalink
fixes #131 by preferring string overloaded methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Sep 10, 2018
1 parent d503eec commit 268b719
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ bld/
# Visual Studo 2015 cache/options directory
.vs/

# JetBrains project files
.idea/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,16 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
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)))
.OrderByDescending(m =>
{
var matchingArgs = m.GetParameters().Where(p => suppliedArgumentValues.Any(s => s.Key == p.Name)).ToList();

// Prefer the configuration method with most number of matching arguments and of those the ones with
// the most string type parameters to predict best match with least type casting
return new Tuple<int, int>(
matchingArgs.Count,
matchingArgs.Count(p => p.ParameterType == typeof(string)));
})
.FirstOrDefault();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,20 @@ public void MethodsAreSelectedBasedOnCountOfMatchedArguments()
var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArguments);
Assert.Equal(typeof(ITextFormatter), selected.GetParameters()[1].ParameterType);
}

[Fact]
public void MethodsAreSelectedBasedOnCountOfMatchedArgumentsAndThenStringType()
{
var options = typeof(DummyLoggerConfigurationWithMultipleMethodsExtensions).GetTypeInfo().DeclaredMethods.ToList();
Assert.Equal(3, options.Count(mi => mi.Name == "DummyRollingFile"));
var suppliedArguments = new Dictionary<string, IConfigurationArgumentValue>()
{
{ "pathFormat", new StringArgumentValue(() => "C:\\") },
{ "formatter", new StringArgumentValue(() => "SomeFormatter, SomeAssembly") }
};

var selected = ConfigurationReader.SelectConfigurationMethod(options, "DummyRollingFile", suppliedArguments);
Assert.Equal(typeof(string), selected.GetParameters()[2].ParameterType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Formatting;

namespace Serilog.Settings.Configuration.Tests
{
using System.Collections.Generic;

static class DummyLoggerConfigurationWithMultipleMethodsExtensions
{
public static LoggerConfiguration DummyRollingFile(
LoggerSinkConfiguration loggerSinkConfiguration,
ITextFormatter formatter,
IEnumerable<string> pathFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = null,
IFormatProvider formatProvider = null)
{
return null;
}

public static LoggerConfiguration DummyRollingFile(
LoggerSinkConfiguration loggerSinkConfiguration,
ITextFormatter formatter,
string pathFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = null,
IFormatProvider formatProvider = null)
{
return null;
}

public static LoggerConfiguration DummyRollingFile(
LoggerSinkConfiguration loggerSinkConfiguration,
string pathFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
{
return null;
}
}
}

0 comments on commit 268b719

Please sign in to comment.