Skip to content

fixes #131 by preferring string overloaded methods #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -350,7 +350,15 @@ internal static MethodInfo SelectConfigurationMethod(IEnumerable<MethodInfo> can
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))))
{
var matchingArgs = m.GetParameters().Where(p => suppliedArgumentValues.Any(s => s.Key.Equals(p.Name, StringComparison.OrdinalIgnoreCase))).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;
}
}
}