using GisConverter.Lib.Logging; using Microsoft.Extensions.Logging; namespace GisConverter.TestsApp.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(object? message) { try { _logger.LogDebug(message?.ToString() ?? string.Empty); } catch { /* swallow - logging must be best-effort */ } } /// public void Info(object? message) { try { _logger.LogInformation((string?)message); } catch { /* swallow - logging must be best-effort */ } } /// public void Warn(object? message) { try { _logger.LogWarning(message?.ToString() ?? string.Empty); } catch { /* swallow - logging must be best-effort */ } } /// public void Error(object? message, Exception? ex = null) { try { if (ex != null) _logger.LogError(ex, message?.ToString() ?? string.Empty); else _logger.LogError(message?.ToString() ?? string.Empty); } catch { /* swallow - logging must be best-effort */ } } } }