Skip to content

Support simple type names for Serilog types (fixes #81) #161

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 1 commit into from
Jan 7, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext)

// maybe it's the assembly-qualified type name of a concrete implementation
// with a default constructor
var type = Type.GetType(argumentValue.Trim());
var type = FindType(argumentValue.Trim());
if (type != null)
{
var ctor = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(ci =>
Expand Down Expand Up @@ -142,6 +142,20 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext)
return Convert.ChangeType(argumentValue, toType);
}

internal static Type FindType(string typeName)
{
var type = Type.GetType(typeName);
if (type == null)
{
if (!typeName.Contains(','))
{
type = Type.GetType($"{typeName}, Serilog");
}
}

return type;
}

internal static bool TryParseStaticMemberAccessor(string input, out string accessorTypeName, out string memberName)
{
if (input == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public void TryParseStaticMemberAccessorReturnsExpectedResults(string input, str
}
}

[Theory]
[InlineData("Serilog.Formatting.Json.JsonFormatter", typeof(JsonFormatter))]
[InlineData("Serilog.Formatting.Json.JsonFormatter, Serilog", typeof(JsonFormatter))]
[InlineData("Serilog.ConfigurationLoggerConfigurationExtensions", typeof(ConfigurationLoggerConfigurationExtensions))]
public void FindTypeSupportsSimpleNamesForSerilogTypes(string input, Type targetType)
{
var type = StringArgumentValue.FindType(input);
Assert.Equal(targetType, type);
}

[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractProperty, Serilog.Settings.Configuration.Tests", typeof(AnAbstractClass))]
Expand Down