Skip to content

Commit

Permalink
a converter can implement IConvertTo AND IConvertFrom (fixes #183)
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Sep 19, 2024
1 parent 8d7b03b commit 13d4e6d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 45 deletions.
2 changes: 1 addition & 1 deletion scripts/build-preview.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dotnet build -c Release '-p:GeneratePackages=true;PackageVersion=3.0.1-preview.1' $PSScriptRoot/../src/log4net/log4net.csproj
dotnet build -c Release '-p:GeneratePackages=true;PackageVersion=3.0.1-preview.2' $PSScriptRoot/../src/log4net/log4net.csproj
2 changes: 0 additions & 2 deletions src/log4net.Tests/Appender/FileAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public void FilenameWithPatternStringTest()
""");
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
XmlConfigurator.Configure(rep, log4netConfig["log4net"]!);
ILog log = LogManager.GetLogger(rep.Name, "GeneralFileAppender");
log.Info("Message");
}
finally
{
Expand Down
55 changes: 14 additions & 41 deletions src/log4net/Util/TypeConverters/ConverterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ namespace log4net.Util.TypeConverters
/// </summary>
/// <remarks>
/// <para>
/// Maintains a registry of type converters used to convert between
/// types.
/// Maintains a registry of type converters used to convert between types.
/// </para>
/// <para>
/// Use the <see cref="M:AddConverter(Type, object)"/> and
Expand All @@ -44,13 +43,8 @@ namespace log4net.Util.TypeConverters
public static class ConverterRegistry
{
/// <summary>
/// Static constructor.
/// This class constructor adds the intrinsic type converters
/// </summary>
/// <remarks>
/// <para>
/// This constructor defines the intrinsic type converters.
/// </para>
/// </remarks>
static ConverterRegistry()
{
// Add predefined converters here
Expand All @@ -67,23 +61,19 @@ static ConverterRegistry()
/// </summary>
/// <param name="destinationType">The type being converted to.</param>
/// <param name="converter">The type converter to use to convert to the destination type.</param>
/// <remarks>
/// <para>
/// Adds a converter instance for a specific type.
/// </para>
/// </remarks>
public static void AddConverter(Type? destinationType, object? converter)
{
if (destinationType is not null && converter is not null)
if (destinationType is null || converter is null)
{
if (converter is IConvertTo convertTo)
{
s_type2ConvertTo[destinationType] = convertTo;
}
else if (converter is IConvertFrom convertFrom)
{
s_type2ConvertFrom[destinationType] = convertFrom;
}
return;
}
if (converter is IConvertTo convertTo)
{
s_type2ConvertTo[destinationType] = convertTo;
}
if (converter is IConvertFrom convertFrom)
{
s_type2ConvertFrom[destinationType] = convertFrom;
}
}

Expand All @@ -92,15 +82,8 @@ public static void AddConverter(Type? destinationType, object? converter)
/// </summary>
/// <param name="destinationType">The type being converted to.</param>
/// <param name="converterType">The type of the type converter to use to convert to the destination type.</param>
/// <remarks>
/// <para>
/// Adds a converter <see cref="Type"/> for a specific type.
/// </para>
/// </remarks>
public static void AddConverter(Type destinationType, Type converterType)
{
AddConverter(destinationType, CreateConverterInstance(converterType));
}
=> AddConverter(destinationType, CreateConverterInstance(converterType));

/// <summary>
/// Gets the type converter to use to convert values to the destination type.
Expand All @@ -111,11 +94,6 @@ public static void AddConverter(Type destinationType, Type converterType)
/// The type converter instance to use for type conversions or <c>null</c>
/// if no type converter is found.
/// </returns>
/// <remarks>
/// <para>
/// Gets the type converter to use to convert values to the destination type.
/// </para>
/// </remarks>
public static IConvertTo? GetConvertTo(Type sourceType, Type destinationType)
{
// TODO: Support inheriting type converters.
Expand Down Expand Up @@ -146,11 +124,6 @@ public static void AddConverter(Type destinationType, Type converterType)
/// The type converter instance to use for type conversions or <c>null</c>
/// if no type converter is found.
/// </returns>
/// <remarks>
/// <para>
/// Gets the type converter to use to convert values to the destination type.
/// </para>
/// </remarks>
public static IConvertFrom? GetConvertFrom(Type destinationType)
{
// TODO: Support inheriting type converters.
Expand Down Expand Up @@ -248,4 +221,4 @@ public static void AddConverter(Type destinationType, Type converterType)
private static readonly ConcurrentDictionary<Type, IConvertTo> s_type2ConvertTo = new();
private static readonly ConcurrentDictionary<Type, IConvertFrom> s_type2ConvertFrom = new();
}
}
}
3 changes: 2 additions & 1 deletion src/site/xdoc/release/release-notes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ limitations under the License.
<section id="a3.0.1-bug" name="Bug fixes">
<ul>
<li>
<a href="https://github.com/apache/logging-log4net/issues/tbd">tbd</a> (by tbd)
<a href="https://github.com/apache/logging-log4net/issues/183">Unable to set property [file] on object [log4net.Appender.FileAppender]</a>
(reported by @sc-mk fixed by @FreeAndNil in https://github.com/apache/logging-log4net/pull/184)
</li>
</ul>
</section><section id="a3.0.1-enhancements" name="Enhancements">
Expand Down

0 comments on commit 13d4e6d

Please sign in to comment.