Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Ical.Net.Tests/Ical.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
<NoWarn>$(NoWarn);CS8600;CS8601;CS8602;CS8603;CS8604;CS8618;CS8620;CS8714</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="NLog" Version="6.0.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="6.0.2" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0" PrivateAssets="all" />
Expand Down
38 changes: 38 additions & 0 deletions Ical.Net.Tests/Logging/Adapters/MicrosoftLoggerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright ical.net project maintainers and contributors.
// Licensed under the MIT license.
//
#nullable enable
using System;
using Microsoft.Extensions.Logging;

namespace Ical.Net.Tests.Logging.Adapters;

#pragma warning disable CA2254 // template should be a static expression

internal class MicrosoftLoggerAdapter(ILogger msLogger) : Ical.Net.Logging.ILogger
{
public void Log(Ical.Net.Logging.LogLevel level, string messageTemplate, params object[] args)
=> msLogger.Log(ConvertLogLevel(level), messageTemplate, args);

public void Log(Ical.Net.Logging.LogLevel level, Exception exception, string messageTemplate, params object[] args)
=> msLogger.Log(ConvertLogLevel(level), exception, messageTemplate, args);

public bool IsEnabled(Ical.Net.Logging.LogLevel level) =>
msLogger.IsEnabled(ConvertLogLevel(level));

private static LogLevel ConvertLogLevel(Ical.Net.Logging.LogLevel level)
{
return level switch
{
Ical.Net.Logging.LogLevel.Trace => LogLevel.Trace,
Ical.Net.Logging.LogLevel.Debug => LogLevel.Debug,
Ical.Net.Logging.LogLevel.Information => LogLevel.Information,
Ical.Net.Logging.LogLevel.Warning => LogLevel.Warning,
Ical.Net.Logging.LogLevel.Error => LogLevel.Error,
Ical.Net.Logging.LogLevel.Critical => LogLevel.Critical,
_ => LogLevel.None
};
}
}
#pragma warning restore CA2254
67 changes: 67 additions & 0 deletions Ical.Net.Tests/Logging/Adapters/MicrosoftLoggerFactoryAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Copyright ical.net project maintainers and contributors.
// Licensed under the MIT license.
//
#nullable enable
using System;

namespace Ical.Net.Tests.Logging.Adapters;

/// <summary>
/// Adapts an <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/>
/// to the <see cref="Ical.Net.Logging.ILoggerFactory"/> interface.
/// </summary>
/// <remarks>This class provides a bridge between the <see cref="Microsoft.Extensions.Logging"/> framework and the
/// <see cref="Ical.Net.Logging.ILoggerFactory"/> abstraction. It allows the creation of
/// <see cref="Ical.Net.Logging.ILoggerFactory"/> instances using the underlying
/// <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/>.
/// </remarks>
internal class MicrosoftLoggerFactoryAdapter : Ical.Net.Logging.ILoggerFactory
{
private readonly Microsoft.Extensions.Logging.ILoggerFactory _msLoggerFactory;
private bool _disposed;

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftLoggerFactoryAdapter"/> class, using the specified
/// Microsoft.Extensions.Logging <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/>.
/// </summary>
/// <param name="msLoggerFactory">The <see cref="Microsoft.Extensions.Logging.ILoggerFactory"/> instance used to create loggers.
/// This parameter cannot be <see langword="null"/>.
/// </param>
public MicrosoftLoggerFactoryAdapter(Microsoft.Extensions.Logging.ILoggerFactory msLoggerFactory)
{
_msLoggerFactory = msLoggerFactory;
}

/// <summary>
/// Creates a logger instance for the specified category name.
/// </summary>
/// <param name="categoryName">The name of the category for the logger.</param>
/// <returns>A <see cref="Ical.Net.Logging.ILogger"/> instance configured to log messages for the specified category.</returns>
public Ical.Net.Logging.ILogger CreateLogger(string categoryName)
{
var msLogger = _msLoggerFactory.CreateLogger(categoryName);
return new MicrosoftLoggerAdapter(msLogger);
}

protected virtual void Dispose(bool disposing)
{
if (_disposed) return;

if (disposing)
_msLoggerFactory.Dispose();
_disposed = true;
}

/// <inheritdoc/>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we suppress the finalizer?

}

~MicrosoftLoggerFactoryAdapter()
{
Dispose(false);
}
}
14 changes: 14 additions & 0 deletions Ical.Net.Tests/Logging/Filter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright ical.net project maintainers and contributors.
// Licensed under the MIT license.

#nullable enable
using Microsoft.Extensions.Logging;

namespace Ical.Net.Tests.Logging;

internal record Filter
{
public LogLevel MinLogLevel { get; set; } = LogLevel.Trace;
public LogLevel MaxLogLevel { get; set; } = LogLevel.Critical;
public string LoggerNamePattern { get; set; } = "*";
}
Loading
Loading