using Microsoft.Extensions.Logging;
namespace GitConverter.Lib.Logging
{
///
/// Adapter that forwards library calls to a Microsoft.Extensions.Logging .
///
///
/// - Use this adapter when consumers of the library provide an (for example via DI).
/// - The adapter keeps the library decoupled from Microsoft.Extensions.Logging by implementing the library's
/// interface and forwarding calls to the provided .
/// - Logging calls are guarded with try/catch and swallow exceptions to ensure logging never breaks application flow.
/// - The adapter does not format messages; it forwards the string received from callers to the underlying logger.
///
public sealed class MsLoggerAdapter : IAppLogger
{
private readonly ILogger _logger;
///
/// Create a new adapter that forwards calls to the provided .
///
/// Non-null instance supplied by the host.
/// Thrown when is null.
public MsLoggerAdapter(ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
///
public void Debug(string message)
{
try { _logger.LogDebug(message); } catch { /* swallow - logging must be best-effort */ }
}
///
public void Info(string message)
{
try { _logger.LogInformation(message); } catch { /* swallow - logging must be best-effort */ }
}
///
public void Warn(string message)
{
try { _logger.LogWarning(message); } catch { /* swallow - logging must be best-effort */ }
}
///
public void Error(string message, Exception ex = null)
{
try
{
if (ex != null) _logger.LogError(ex, message);
else _logger.LogError(message);
}
catch { /* swallow - logging must be best-effort */ }
}
}
}