diff --git a/src/Serilog/Settings/KeyValuePairs/SettingValueConversions.cs b/src/Serilog/Settings/KeyValuePairs/SettingValueConversions.cs index 519fea315..662fe60d4 100644 --- a/src/Serilog/Settings/KeyValuePairs/SettingValueConversions.cs +++ b/src/Serilog/Settings/KeyValuePairs/SettingValueConversions.cs @@ -51,7 +51,7 @@ public static object ConvertToType(string value, Type toType) if (convertor != null) return convertor(value); - if (toTypeInfo.IsInterface && !string.IsNullOrWhiteSpace(value)) + if ((toTypeInfo.IsInterface || toTypeInfo.IsAbstract) && !string.IsNullOrWhiteSpace(value)) { var type = Type.GetType(value.Trim(), throwOnError: false); if (type != null) diff --git a/test/Serilog.Tests/Settings/SettingValueConversionsTests.cs b/test/Serilog.Tests/Settings/SettingValueConversionsTests.cs index 17d2cfcfc..586ba748a 100644 --- a/test/Serilog.Tests/Settings/SettingValueConversionsTests.cs +++ b/test/Serilog.Tests/Settings/SettingValueConversionsTests.cs @@ -3,6 +3,7 @@ using Serilog.Formatting; using Serilog.Formatting.Json; using Serilog.Settings.KeyValuePairs; +using Serilog.Tests.Support; using Xunit; namespace Serilog.Tests.Settings @@ -51,6 +52,21 @@ public void StringValuesConvertToDefaultInstancesIfTargetIsInterface() Assert.IsType(result); } + [Fact] + public void StringValuesConvertToDefaultInstancesIfTargetIsAbstractClass() + { + var result = SettingValueConversions.ConvertToType("Serilog.Tests.Support.DummyConcreteClassWithDefaultConstructor, Serilog.Tests", typeof(DummyAbstractClass)); + Assert.IsType(result); + } + + [Fact] + public void StringValuesThrowsWhenMissingDefaultConstructorIfTargetIsAbstractClass() + { + var value = "Serilog.Tests.Support.DummyConcreteClassWithoutDefaultConstructor, Serilog.Tests"; + Assert.Throws(() => + SettingValueConversions.ConvertToType(value, typeof(DummyAbstractClass))); + } + [Theory] [InlineData("3.14:21:18.986", 3 /*days*/, 14 /*hours*/, 21 /*min*/, 18 /*sec*/, 986 /*ms*/)] [InlineData("4", 4, 0, 0, 0, 0)] // minimal : days diff --git a/test/Serilog.Tests/Support/ClassHierarchy.cs b/test/Serilog.Tests/Support/ClassHierarchy.cs new file mode 100644 index 000000000..554fcf94a --- /dev/null +++ b/test/Serilog.Tests/Support/ClassHierarchy.cs @@ -0,0 +1,23 @@ +namespace Serilog.Tests.Support +{ + + public abstract class DummyAbstractClass + { + } + + public class DummyConcreteClassWithDefaultConstructor + { + // ReSharper disable once UnusedParameter.Local + public DummyConcreteClassWithDefaultConstructor(string param = "") + { + } + } + + public class DummyConcreteClassWithoutDefaultConstructor + { + // ReSharper disable once UnusedParameter.Local + public DummyConcreteClassWithoutDefaultConstructor(string param) + { + } + } +}