Files
Telegrator/src/Telegrator.Hosting/Logging/MicrosoftLoggingAdapter.cs
T

45 lines
1.6 KiB
C#
Raw Normal View History

2026-03-09 13:23:21 +04:00
namespace Telegrator.Logging;
2026-03-09 13:23:21 +04:00
/// <summary>
/// Adapter for Microsoft.Extensions.Logging to work with Telegrator logging system.
/// This allows seamless integration with ASP.NET Core logging infrastructure.
/// </summary>
public class MicrosoftLoggingAdapter : ITelegratorLogger
{
2026-03-09 13:23:21 +04:00
private readonly Microsoft.Extensions.Logging.ILogger _logger;
/// <summary>
2026-03-09 13:23:21 +04:00
/// Initializes a new instance of MicrosoftLoggingAdapter.
/// </summary>
2026-03-09 13:23:21 +04:00
/// <param name="logger">The Microsoft.Extensions.Logging logger instance.</param>
public MicrosoftLoggingAdapter(Microsoft.Extensions.Logging.ILogger logger)
{
2026-03-09 13:23:21 +04:00
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
2026-03-09 13:23:21 +04:00
/// <inheritdoc/>
public void Log(LogLevel level, string message, Exception? exception = null)
{
var msLogLevel = level switch
{
2026-03-09 13:23:21 +04:00
LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
LogLevel.Information => Microsoft.Extensions.Logging.LogLevel.Information,
LogLevel.Warning => Microsoft.Extensions.Logging.LogLevel.Warning,
LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
_ => Microsoft.Extensions.Logging.LogLevel.Information
};
2026-03-09 13:23:21 +04:00
if (exception != null)
{
2026-04-27 16:28:20 +04:00
_logger.Log(msLogLevel, default, message, exception,
(str, exc) => string.Format("{0} : {1}", str, exc));
2026-03-09 13:23:21 +04:00
}
else
{
2026-04-27 16:28:20 +04:00
_logger.Log(msLogLevel, default, message, null,
(str, _) => str);
}
}
2026-03-09 13:23:21 +04:00
}