Skip to content

Commit

Permalink
LogLevel - Added support for TypeConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Jun 10, 2019
1 parent 3d07bd9 commit b8f6070
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 6 deletions.
102 changes: 102 additions & 0 deletions src/NLog/Attributes/LogLevelTypeConverter.cs
@@ -0,0 +1,102 @@
//
// 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 IsSupportedType(sourceType) || base.CanConvertFrom(context, sourceType);
}

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
TypeCode casted = Convert.GetTypeCode(value);
switch (casted)
{
case TypeCode.String:
return LogLevel.FromString(value.ToString());
case TypeCode.Int16:
return LogLevel.FromOrdinal(Convert.ToInt16(value));
case TypeCode.UInt16:
return LogLevel.FromOrdinal(Convert.ToUInt16(value));
case TypeCode.Int32:
return LogLevel.FromOrdinal(Convert.ToInt32(value));
case TypeCode.UInt32:
return LogLevel.FromOrdinal((int)Convert.ToUInt32(value));
case TypeCode.Int64:
return LogLevel.FromOrdinal((int)Convert.ToInt64(value));
case TypeCode.UInt64:
return LogLevel.FromOrdinal((int)Convert.ToUInt64(value));
default:
return base.ConvertFrom(context, culture, value);
}
}

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

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var casted = value as LogLevel;
return destinationType == typeof(string) && casted != null
? casted.ToString()
: base.ConvertTo(context, culture, casted?.Ordinal ?? value, destinationType);
}

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

private static bool ConvertToString(Type objectType)
private static bool ConvertSimpleToString(Type objectType)
{
if (objectType == typeof(Guid))
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 @@ -293,6 +293,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
7 changes: 5 additions & 2 deletions tests/NLog.UnitTests/LayoutRenderers/LogLevelTests.cs
Expand Up @@ -132,16 +132,19 @@ 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;

// Act
var result = Convert.ChangeType(logLevel, type);
var changeToResult = Convert.ChangeType(logLevel, type);
var changeFromResult = System.ComponentModel.TypeDescriptor.GetConverter(typeof(LogLevel)).ConvertFrom(changeToResult);

// Assert
Assert.Equal(expected, result);
Assert.Equal(expected, changeToResult);
Assert.Equal(logLevel, changeFromResult);
}
}
}

0 comments on commit b8f6070

Please sign in to comment.