* Added Logging abstraction for base library

* Version incremented
This commit is contained in:
2025-08-03 03:29:15 +04:00
parent 34ac0231d5
commit a87a07d939
13 changed files with 374 additions and 128 deletions
@@ -0,0 +1,45 @@
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
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
/// <summary>
/// Initializes a new instance of MicrosoftLoggingAdapter.
/// </summary>
/// <param name="logger">The Microsoft.Extensions.Logging logger instance.</param>
public MicrosoftLoggingAdapter(Microsoft.Extensions.Logging.ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <inheritdoc/>
public void Log(LogLevel level, string message, Exception? exception = null)
{
var msLogLevel = level switch
{
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
};
if (exception != null)
{
_logger.Log(msLogLevel, default, exception, message);
}
else
{
_logger.Log(msLogLevel, message);
}
}
}
}
+2
View File
@@ -5,6 +5,8 @@ using System.Text;
using Telegram.Bot.Types.Enums;
using Telegrator.Configuration;
using Telegrator.Hosting.Components;
using Telegrator.Hosting.Logging;
using Telegrator.Logging;
using Telegrator.Hosting.Providers;
using Telegrator.MadiatorCore;
using Telegrator.MadiatorCore.Descriptors;
+1 -1
View File
@@ -16,7 +16,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.0.9</Version>
<Version>1.0.10</Version>
</PropertyGroup>
<ItemGroup>
+15
View File
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
@@ -9,8 +10,10 @@ using Telegram.Bot.Types;
using Telegrator.Configuration;
using Telegrator.Hosting.Components;
using Telegrator.Hosting.Configuration;
using Telegrator.Hosting.Logging;
using Telegrator.Hosting.Polling;
using Telegrator.Hosting.Providers;
using Telegrator.Logging;
using Telegrator.MadiatorCore;
namespace Telegrator.Hosting
@@ -92,6 +95,18 @@ namespace Telegrator.Hosting
client.SetMyCommands(aliases).Wait();
return botHost;
}
/// <summary>
/// Adds a Microsoft.Extensions.Logging adapter to Alligator using a logger factory.
/// </summary>
/// <param name="host"></param>
public static void AddLoggingAdapter(this ITelegramBotHost host)
{
ILoggerFactory loggerFactory = host.Services.GetRequiredService<ILoggerFactory>();
ILogger logger = loggerFactory.CreateLogger("Telegrator");
MicrosoftLoggingAdapter adapter = new MicrosoftLoggingAdapter(logger);
Alligator.AddAdapter(adapter);
}
}
/// <summary>