2025-08-12 00:09:45 +04:00
|
|
|
using Microsoft.Extensions.Logging;
|
2025-08-03 03:29:15 +04:00
|
|
|
using Telegrator.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Telegrator.Hosting.Logging
|
|
|
|
|
{
|
|
|
|
|
/// <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
|
|
|
|
|
{
|
2025-08-12 00:09:45 +04:00
|
|
|
private readonly ILogger _logger;
|
2025-08-03 03:29:15 +04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of MicrosoftLoggingAdapter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The Microsoft.Extensions.Logging logger instance.</param>
|
2025-08-12 00:09:45 +04:00
|
|
|
public MicrosoftLoggingAdapter(ILogger logger)
|
2025-08-03 03:29:15 +04:00
|
|
|
{
|
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-08-12 00:09:45 +04:00
|
|
|
public void Log(Telegrator.Logging.LogLevel level, string message, Exception? exception = null)
|
2025-08-03 03:29:15 +04:00
|
|
|
{
|
|
|
|
|
var msLogLevel = level switch
|
|
|
|
|
{
|
2025-08-12 00:09:45 +04:00
|
|
|
Telegrator.Logging.LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
|
|
|
|
|
Telegrator.Logging.LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
|
|
|
|
|
Telegrator.Logging.LogLevel.Information => Microsoft.Extensions.Logging.LogLevel.Information,
|
|
|
|
|
Telegrator.Logging.LogLevel.Warning => Microsoft.Extensions.Logging.LogLevel.Warning,
|
|
|
|
|
Telegrator.Logging.LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
|
2025-08-03 03:29:15 +04:00
|
|
|
_ => Microsoft.Extensions.Logging.LogLevel.Information
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (exception != null)
|
|
|
|
|
{
|
2025-08-03 03:42:27 +04:00
|
|
|
_logger.Log(msLogLevel, default, message, exception, (str, exc) => string.Format("{0} : {1}", str, exc));
|
2025-08-03 03:29:15 +04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-08-03 03:42:27 +04:00
|
|
|
_logger.Log(msLogLevel, default, message, null, (str, _) => str);
|
2025-08-03 03:29:15 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|