Skip to content
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

LogLevel - Added support for TypeConverter #3469

Merged
merged 2 commits into from Jun 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/NLog/Attributes/LogLevelTypeConverter.cs
@@ -0,0 +1,91 @@
//
// Copyright (c) 2004-2019 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

namespace NLog.Attributes
{
using System;
using System.ComponentModel;
using System.Globalization;

/// <summary>
/// Support <see cref="NLog.LogLevel"/> implementation of <see cref="IConvertible"/>
/// </summary>
public class LogLevelTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || IsNumericType(sourceType) || base.CanConvertFrom(context, sourceType);
}

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value?.GetType() == typeof(string))
return LogLevel.FromString(value.ToString());
else if (IsNumericType(value?.GetType()))
return LogLevel.FromOrdinal(Convert.ToInt32(value));
else
return base.ConvertFrom(context, culture, value);
}

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || IsNumericType(destinationType) || base.CanConvertTo(context, destinationType);
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is LogLevel logLevel)
{
if (destinationType == typeof(string))
return logLevel.ToString();
else if (IsNumericType(destinationType))
return Convert.ChangeType(logLevel.Ordinal, destinationType, culture);
}
return base.ConvertTo(context, culture, value, destinationType);
}

private static bool IsNumericType(Type sourceType)
{
return sourceType == typeof(int) ||
sourceType == typeof(uint) ||
sourceType == typeof(long) ||
sourceType == typeof(ulong) ||
sourceType == typeof(short) ||
sourceType == typeof(ushort);
}
}
}
4 changes: 2 additions & 2 deletions src/NLog/Internal/ObjectReflectionCache.cs
Expand Up @@ -149,7 +149,7 @@ private static bool TryExtractExpandoObject(Type objectType, out ObjectPropertyI
private static ObjectPropertyInfos BuildObjectPropertyInfos(object value, Type objectType)
{
ObjectPropertyInfos propertyInfos;
if (ConvertToString(objectType))
if (ConvertSimpleToString(objectType))
{
propertyInfos = ObjectPropertyInfos.SimpleToString;
}
Expand All @@ -175,7 +175,7 @@ private static ObjectPropertyInfos BuildObjectPropertyInfos(object value, Type o
return propertyInfos;
}

private static bool ConvertToString(Type objectType)
private static bool ConvertSimpleToString(Type objectType)
{
if (typeof(IFormattable).IsAssignableFrom(objectType))
return true;
Expand Down
8 changes: 6 additions & 2 deletions src/NLog/LogLevel.cs
Expand Up @@ -35,11 +35,12 @@ namespace NLog
{
using System;
using System.Collections.Generic;
using NLog.Internal;
using System.ComponentModel;

/// <summary>
/// Defines available log levels.
/// </summary>
[TypeConverter(typeof(Attributes.LogLevelTypeConverter))]
public sealed class LogLevel : IComparable, IEquatable<LogLevel>, IConvertible
{
/// <summary>
Expand Down Expand Up @@ -464,7 +465,10 @@ string IConvertible.ToString(IFormatProvider provider)

object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
return Convert.ChangeType(_ordinal, conversionType, provider);
if (conversionType == typeof(string))
return Name;
else
return Convert.ChangeType(_ordinal, conversionType, provider);
}

ushort IConvertible.ToUInt16(IFormatProvider provider)
Expand Down
1 change: 1 addition & 0 deletions src/NLog/NLog.csproj
Expand Up @@ -290,6 +290,7 @@ For all config options and platform support, check https://nlog-project.org/conf
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.0.0" />
<PackageReference Include="System.ComponentModel.Primitives" Version="4.1.0" />
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.1.0" />
<PackageReference Include="System.Data.Common" Version="4.1.0" />
<PackageReference Include="System.Diagnostics.StackTrace" Version="4.0.1" />
<PackageReference Include="System.Net.NameResolution" Version="4.0.0" />
Expand Down
16 changes: 12 additions & 4 deletions tests/NLog.UnitTests/LayoutRenderers/LogLevelTests.cs
Expand Up @@ -132,16 +132,24 @@ public void LogLevelGetTypeCodeTest()
[Theory]
[InlineData(typeof(int), 2)]
[InlineData(typeof(uint), (uint)2)]
[InlineData(typeof(string), "Info")]
public void LogLevelConvertTest(Type type, object expected)
{
// Arrange
var logLevel = LogLevel.Info;

IConvertible logLevel = LogLevel.Info;
var logConverter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(LogLevel));

// Act
var result = Convert.ChangeType(logLevel, type);
var changeTypeResult = Convert.ChangeType(logLevel, type);
var changeToResult = logLevel.ToType(type, System.Globalization.CultureInfo.CurrentCulture);
var convertToResult = logConverter.CanConvertTo(type) ? logConverter.ConvertTo(logLevel, type) : null;
var convertFromResult = logConverter.CanConvertFrom(expected.GetType()) ? logConverter.ConvertFrom(expected) : null;

// Assert
Assert.Equal(expected, result);
Assert.Equal(expected, changeTypeResult);
Assert.Equal(expected, changeToResult);
Assert.Equal(expected, convertToResult);
Assert.Equal(logLevel, convertFromResult);
}
}
}