using Microsoft.Extensions.Logging;
using GitConverter.Lib.Logging;
namespace GitConverter.TestsApp.Logging
{
///
/// Unit tests for .
///
///
/// - These tests exercise that messages sent through the adapter reach the underlying Microsoft.Extensions.Logging
/// as formatted strings.
/// - A lightweight implementing collects formatted messages for assertions,
/// avoiding any external dependencies.
/// - Tests are tolerant to minor formatting differences by using case-insensitive containment assertions.
///
public partial class MsLoggerAdapterTests
{
// Minimal disposable scope returned by BeginScope
private sealed class NullScope : IDisposable
{
public static readonly NullScope Instance = new NullScope();
private NullScope() { }
public void Dispose() { }
}
///
/// A simple that collects formatted messages for assertions.
///
private class CollectingLogger : ILogger
{
public List Messages { get; } = new();
public IDisposable BeginScope(TState state) => NullScope.Instance;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)
{
var msg = formatter != null ? formatter(state, exception) : state?.ToString();
if (!string.IsNullOrEmpty(msg)) Messages.Add(msg);
}
}
///
/// Verifies the adapter forwards Info/Debug/Warn/Error calls to the underlying .
///
[Fact]
public void MsLoggerAdapter_ForwardsMessages()
{
var msLogger = new CollectingLogger();
var adapter = new MsLoggerAdapter(msLogger);
adapter.Info("hello");
adapter.Debug("dbg");
adapter.Warn("warn");
adapter.Error("err", null);
Assert.True(msLogger.Messages.Any(m => m.IndexOf("hello", StringComparison.OrdinalIgnoreCase) >= 0));
Assert.True(msLogger.Messages.Any(m => m.IndexOf("dbg", StringComparison.OrdinalIgnoreCase) >= 0));
Assert.True(msLogger.Messages.Any(m => m.IndexOf("warn", StringComparison.OrdinalIgnoreCase) >= 0));
Assert.True(msLogger.Messages.Any(m => m.IndexOf("err", StringComparison.OrdinalIgnoreCase) >= 0));
}
}
}