Skip to content

Commit

Permalink
NLogConfigurationException - Improve styling of error-message when fa…
Browse files Browse the repository at this point in the history
…iling to assign config-property (NLog#4809)
  • Loading branch information
snakefoot committed Feb 26, 2022
1 parent e8fe5c0 commit 831bad5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/NLog/Config/LoggingConfigurationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ private void SetPropertyValueFromString(object targetObject, string propertyName
if (ex.MustBeRethrownImmediately())
throw;

var configException = new NLogConfigurationException($"Error when setting value '{propertyValue}' for property '{propertyName}' on {targetObject?.GetType()} in section '{element.Name}'", ex);
var configException = new NLogConfigurationException($"'{targetObject?.GetType()?.Name}' cannot assign property '{propertyName}'='{propertyValue}' in section '{element.Name}'. Error: {ex.Message}", ex);
if (MustThrowConfigException(configException))
throw;
}
Expand All @@ -1090,7 +1090,7 @@ private void SetPropertyValuesFromElement(object o, ValidatedConfigurationElemen
{
if (!PropertyHelper.TryGetPropertyInfo(o, childElement.Name, out var propInfo))
{
var configException = new NLogConfigurationException($"Unknown property '{childElement.Name}' for '{o?.GetType()}' in section '{parentElement.Name}'");
var configException = new NLogConfigurationException($"'{o?.GetType()?.Name}' cannot assign unknown property '{childElement.Name}' in section '{parentElement.Name}'");
if (MustThrowConfigException(configException))
throw configException;

Expand Down
8 changes: 4 additions & 4 deletions src/NLog/Internal/Reflection/PropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal static void SetPropertyFromString(object obj, string propertyName, stri

if (!TryGetPropertyInfo(objType, propertyName, out var propInfo))
{
throw new NLogConfigurationException($"Unknown property '{propertyName}'='{value}' for '{objType.Name}'");
throw new NLogConfigurationException($"'{objType?.Name}' cannot assign unknown property '{propertyName}'='{value}'");
}

SetPropertyFromString(obj, propertyName, value, propInfo, configurationItemFactory);
Expand All @@ -112,7 +112,7 @@ internal static void SetPropertyFromString(object obj, string propertyName, stri
{
if (propInfo.IsDefined(_arrayParameterAttribute.GetType(), false))
{
throw new NotSupportedException($"Property {propertyName} on {obj.GetType().Name} is an array, and cannot be assigned a scalar value: '{value}'.");
throw new NotSupportedException($"'{obj?.GetType()?.Name}' cannot assign property '{propertyName}', because property of type array and not scalar value: '{value}'.");
}

propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
Expand All @@ -128,7 +128,7 @@ internal static void SetPropertyFromString(object obj, string propertyName, stri
}
catch (TargetInvocationException ex)
{
throw new NLogConfigurationException($"Error when setting property '{propInfo.Name}'='{value}' on {obj.GetType().Name}", ex.InnerException);
throw new NLogConfigurationException($"'{obj?.GetType()?.Name}' cannot assign property '{propInfo.Name}'='{value}'", ex.InnerException ?? ex);
}
catch (Exception exception)
{
Expand All @@ -137,7 +137,7 @@ internal static void SetPropertyFromString(object obj, string propertyName, stri
throw;
}

throw new NLogConfigurationException($"Error when setting property '{propInfo.Name}'='{value}' on {obj.GetType().Name}", exception);
throw new NLogConfigurationException($"'{obj?.GetType()?.Name}' cannot assign property '{propInfo.Name}'='{value}'. Error={exception.Message}", exception);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/NLog/Layouts/LayoutParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ private static LayoutRenderer ParseLayoutRenderer(ConfigurationItemFactory confi
var value = ParseParameterValue(stringReader);
if (!string.IsNullOrEmpty(parameterName) || !StringHelpers.IsNullOrWhiteSpace(value))
{
var configException = new NLogConfigurationException($"Unknown property '{parameterName}=' for ${{{typeName}}} ({layoutRenderer?.GetType()})");
var configException = new NLogConfigurationException($"${{{typeName}}} cannot assign unknown property '{parameterName}='");
if (throwConfigExceptions ?? configException.MustBeRethrown())
{
throw configException;
Expand Down Expand Up @@ -522,7 +522,7 @@ private static string ValidatePreviousParameterName(string previousParameterName
{
if (parameterName?.Equals(previousParameterName, StringComparison.OrdinalIgnoreCase) == true)
{
var configException = new NLogConfigurationException($"Same property '{parameterName}' assigned twice for {layoutRenderer?.GetType()} ");
var configException = new NLogConfigurationException($"'{layoutRenderer?.GetType()?.Name}' has same property '{parameterName}=' assigned twice");
if (throwConfigExceptions ?? configException.MustBeRethrown())
{
throw configException;
Expand Down Expand Up @@ -590,7 +590,7 @@ private static string SetDefaultPropertyValue(string value, LayoutRenderer layou
}
else
{
var configException = new NLogConfigurationException($"{layoutRenderer.GetType()} has no default property to assign value {value}");
var configException = new NLogConfigurationException($"'{layoutRenderer?.GetType()?.Name}' has no default property to assign value {value}");
if (throwConfigExceptions ?? configException.MustBeRethrown())
{
throw configException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void AssignArrayPropertyFromStringWillResultInNotSupportedExceptionSomeWh
// Assert
Assert.IsType<NLogConfigurationException>(ex.InnerException);
Assert.IsType<NotSupportedException>(ex.InnerException.InnerException);
Assert.Contains("is an array, and cannot be assigned a scalar value", ex.InnerException.InnerException.Message);
Assert.Contains("because property of type array and not scalar value", ex.InnerException.InnerException.Message);
}
}
}
12 changes: 12 additions & 0 deletions tests/NLog.UnitTests/Layouts/SimpleLayoutParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public void UnknownLayoutRenderer()
Assert.Throws<NLogConfigurationException>(() => new SimpleLayout("'${{unknown-type}}'"));
}

[Fact]
public void UnknownLayoutRendererProperty()
{
Assert.Throws<NLogConfigurationException>(() => new SimpleLayout("'${message:unknown_item=${unknown-value}}'"));
}

[Fact]
public void UnknownLayoutRendererPropertyValue()
{
Assert.Throws<NLogConfigurationException>(() => new SimpleLayout("'${message:withexception=${unknown-value}}'"));
}

[Fact]
public void SingleParamTest()
{
Expand Down

0 comments on commit 831bad5

Please sign in to comment.