Skip to content

Commit ce614e6

Browse files
author
Sergey Komisarchik
authored
Merge pull request #161 from skomis-mm/issue81
Support simple type names for Serilog types (fixes #81)
2 parents 54c6677 + b79d99c commit ce614e6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext)
9797

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

145+
internal static Type FindType(string typeName)
146+
{
147+
var type = Type.GetType(typeName);
148+
if (type == null)
149+
{
150+
if (!typeName.Contains(','))
151+
{
152+
type = Type.GetType($"{typeName}, Serilog");
153+
}
154+
}
155+
156+
return type;
157+
}
158+
145159
internal static bool TryParseStaticMemberAccessor(string input, out string accessorTypeName, out string memberName)
146160
{
147161
if (input == null)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public void TryParseStaticMemberAccessorReturnsExpectedResults(string input, str
7474
}
7575
}
7676

77+
[Theory]
78+
[InlineData("Serilog.Formatting.Json.JsonFormatter", typeof(JsonFormatter))]
79+
[InlineData("Serilog.Formatting.Json.JsonFormatter, Serilog", typeof(JsonFormatter))]
80+
[InlineData("Serilog.ConfigurationLoggerConfigurationExtensions", typeof(ConfigurationLoggerConfigurationExtensions))]
81+
public void FindTypeSupportsSimpleNamesForSerilogTypes(string input, Type targetType)
82+
{
83+
var type = StringArgumentValue.FindType(input);
84+
Assert.Equal(targetType, type);
85+
}
86+
7787
[Theory]
7888
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::InterfaceProperty, Serilog.Settings.Configuration.Tests", typeof(IAmAnInterface))]
7989
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::AbstractProperty, Serilog.Settings.Configuration.Tests", typeof(AnAbstractClass))]

0 commit comments

Comments
 (0)