diff --git a/Telegrator.Hosting/Logging/MicrosoftLoggingAdapter.cs b/Telegrator.Hosting/Logging/MicrosoftLoggingAdapter.cs index c4b4bc1..1caf4d7 100644 --- a/Telegrator.Hosting/Logging/MicrosoftLoggingAdapter.cs +++ b/Telegrator.Hosting/Logging/MicrosoftLoggingAdapter.cs @@ -34,11 +34,11 @@ namespace Telegrator.Hosting.Logging if (exception != null) { - _logger.Log(msLogLevel, default, exception, message); + _logger.Log(msLogLevel, default, message, exception, (str, exc) => string.Format("{0} : {1}", str, exc)); } else { - _logger.Log(msLogLevel, message); + _logger.Log(msLogLevel, default, message, null, (str, _) => str); } } } diff --git a/Telegrator/Configuration/ITelegratorOptions.cs b/Telegrator/Configuration/ITelegratorOptions.cs index 5c0787b..fd220e8 100644 --- a/Telegrator/Configuration/ITelegratorOptions.cs +++ b/Telegrator/Configuration/ITelegratorOptions.cs @@ -6,18 +6,6 @@ /// public interface ITelegratorOptions { - /* - /// - /// Gets or sets a value indicating whether only the first found handler should be executed for each update. - /// - public bool ExecuteOnlyFirstFoundHanlder { get; set; } - - /// - /// Gets or sets a value indicating whether to descend the indexr of handler's index on register. ('false' by default) - /// - public bool DescendDescriptorIndex { get; set; } - */ - /// /// Gets or sets the maximum number of parallel working handlers. Null means no limit. /// diff --git a/Telegrator/Filters/Components/AnonymousCompiledFilter.cs b/Telegrator/Filters/Components/AnonymousCompiledFilter.cs index 7939b6c..2a7a090 100644 --- a/Telegrator/Filters/Components/AnonymousCompiledFilter.cs +++ b/Telegrator/Filters/Components/AnonymousCompiledFilter.cs @@ -1,5 +1,6 @@ using Telegram.Bot; using Telegram.Bot.Types; +using Telegrator.Logging; namespace Telegrator.Filters.Components { @@ -47,7 +48,7 @@ namespace Telegrator.Filters.Components if (!filter.CanPass(context)) { if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter) - Alligator.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); + Alligator.LogDebug("{0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); return false; } diff --git a/Telegrator/Filters/Components/AnonymousTypeFilter.cs b/Telegrator/Filters/Components/AnonymousTypeFilter.cs index 9809a0a..f2bb65e 100644 --- a/Telegrator/Filters/Components/AnonymousTypeFilter.cs +++ b/Telegrator/Filters/Components/AnonymousTypeFilter.cs @@ -1,4 +1,5 @@ using Telegram.Bot.Types; +using Telegrator.Logging; namespace Telegrator.Filters.Components { @@ -49,7 +50,7 @@ namespace Telegrator.Filters.Components if (!filter.CanPass(context)) { if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter) - Alligator.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); + Alligator.LogDebug("{0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); return false; } diff --git a/Telegrator/Filters/Components/CompiledFilter.cs b/Telegrator/Filters/Components/CompiledFilter.cs index 45b723d..cbe4c97 100644 --- a/Telegrator/Filters/Components/CompiledFilter.cs +++ b/Telegrator/Filters/Components/CompiledFilter.cs @@ -1,4 +1,6 @@ -namespace Telegrator.Filters.Components +using Telegrator.Logging; + +namespace Telegrator.Filters.Components { /// /// Represents a filter that composes multiple filters and passes only if all of them pass. @@ -39,7 +41,7 @@ if (!filter.CanPass(context)) { if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter) - Alligator.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); + Alligator.LogDebug("{0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); return false; } diff --git a/Telegrator/Logging/Alligator.cs b/Telegrator/Logging/Alligator.cs index 5ada902..03e2e4a 100644 --- a/Telegrator/Logging/Alligator.cs +++ b/Telegrator/Logging/Alligator.cs @@ -14,6 +14,12 @@ namespace Telegrator.Logging /// public static int AdaptersCount => _adapters.Count; + /// + /// Minimal level of logging messages. + /// Any messages below thi value will not be writen! + /// + public static LogLevel MinimalLevel { get; set; } + /// /// Adds a logger adapter to the centralized logging system. /// @@ -70,6 +76,9 @@ namespace Telegrator.Logging if (_adapters.Count == 0) return; + if (level < MinimalLevel) + return; + // Lock only during enumeration to prevent collection modification during iteration lock (_lock) { @@ -105,6 +114,16 @@ namespace Telegrator.Logging Log(LogLevel.Debug, message); } + /// + /// Logs a debug message to all registered adapters. + /// + /// The message to log. + /// + public static void LogDebug(string message, params object[] args) + { + Log(LogLevel.Debug, string.Format(message, args)); + } + /// /// Logs an information message to all registered adapters. /// @@ -114,6 +133,16 @@ namespace Telegrator.Logging Log(LogLevel.Information, message); } + /// + /// Logs an information message to all registered adapters. + /// + /// The message to log. + /// + public static void LogInformation(string message, params object[] args) + { + Log(LogLevel.Information, string.Format(message, args)); + } + /// /// Logs a warning message to all registered adapters. /// @@ -123,6 +152,16 @@ namespace Telegrator.Logging Log(LogLevel.Warning, message); } + /// + /// Logs a warning message to all registered adapters. + /// + /// The message to log. + /// + public static void LogWarning(string message, params object[] args) + { + Log(LogLevel.Warning, string.Format(message, args)); + } + /// /// Logs an error message to all registered adapters. /// @@ -133,6 +172,16 @@ namespace Telegrator.Logging Log(LogLevel.Error, message, exception); } + /// + /// Logs an error message to all registered adapters. + /// + /// The message to log. + /// + public static void LogError(string message, params object[] args) + { + Log(LogLevel.Error, string.Format(message, args)); + } + /// /// Logs an error message with exception only to all registered adapters. /// diff --git a/Telegrator/MadiatorCore/Descriptors/DescriptorFiltersSet.cs b/Telegrator/MadiatorCore/Descriptors/DescriptorFiltersSet.cs index 461f3aa..66ce1a6 100644 --- a/Telegrator/MadiatorCore/Descriptors/DescriptorFiltersSet.cs +++ b/Telegrator/MadiatorCore/Descriptors/DescriptorFiltersSet.cs @@ -1,5 +1,6 @@ using Telegram.Bot.Types; using Telegrator.Filters.Components; +using Telegrator.Logging; namespace Telegrator.MadiatorCore.Descriptors { @@ -47,7 +48,7 @@ namespace Telegrator.MadiatorCore.Descriptors { if (!UpdateValidator.CanPass(filterContext)) { - Alligator.FilterWriteLine("(E) UpdateValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id); + Alligator.LogDebug("(E) UpdateValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id); return false; } @@ -59,7 +60,7 @@ namespace Telegrator.MadiatorCore.Descriptors { if (!StateKeeperValidator.CanPass(filterContext)) { - Alligator.FilterWriteLine("(E) StateKeeperValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id); + Alligator.LogDebug("(E) StateKeeperValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id); return false; } @@ -74,7 +75,7 @@ namespace Telegrator.MadiatorCore.Descriptors if (!filter.CanPass(filterContext)) { if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter) - Alligator.FilterWriteLine("(E) {0} filter of {1} for Update ({2}) didnt pass!", filter.GetType().Name, filterContext.Data["handler_name"], filterContext.Update.Id); + Alligator.LogDebug("(E) {0} filter of {1} for Update ({2}) didnt pass!", filter.GetType().Name, filterContext.Data["handler_name"], filterContext.Update.Id); return false; } diff --git a/Telegrator/Polling/UpdateRouter.cs b/Telegrator/Polling/UpdateRouter.cs index 5ca4e89..a2591f8 100644 --- a/Telegrator/Polling/UpdateRouter.cs +++ b/Telegrator/Polling/UpdateRouter.cs @@ -6,6 +6,7 @@ using Telegram.Bot.Types.Enums; using Telegrator.Configuration; using Telegrator.Filters.Components; using Telegrator.Handlers.Components; +using Telegrator.Logging; using Telegrator.MadiatorCore; using Telegrator.MadiatorCore.Descriptors; @@ -84,7 +85,7 @@ namespace Telegrator.Polling /// A task representing the asynchronous error handling operation. public virtual Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken) { - Alligator.RouterWriteLine("Handling exception {0}", exception.GetType().Name); + Alligator.LogDebug("Handling exception {0}", exception.GetType().Name); ExceptionHandler?.HandleException(botClient, exception, source, cancellationToken); return Task.CompletedTask; } @@ -99,7 +100,7 @@ namespace Telegrator.Polling public virtual async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { // Logging - Alligator.RouterWriteLine("Received Update ({0}) of type \"{1}\"", update.Id, update.Type); + Alligator.LogDebug("Received Update ({0}) of type \"{1}\"", update.Id, update.Type); LogUpdate(update); try @@ -114,22 +115,22 @@ namespace Telegrator.Polling // Chicking if awaiting handlers has exclusive routing if (Options.ExclusiveAwaitingHandlerRouting) { - Alligator.RouterWriteLine("Receiving Update ({0}) completed with only awaiting handlers", update.Id); + Alligator.LogDebug("Receiving Update ({0}) completed with only awaiting handlers", update.Id); return; } } // Queuing reagular handlers for execution await HandlersPool.Enqueue(GetHandlers(HandlersProvider, this, botClient, update, cancellationToken)); - Alligator.RouterWriteLine("Receiving Update ({0}) finished", update.Id); + Alligator.LogDebug("Receiving Update ({0}) finished", update.Id); } catch (OperationCanceledException) { - Alligator.RouterWriteLine("Receiving Update ({0}) cancelled", update.Id); + Alligator.LogDebug("Receiving Update ({0}) cancelled", update.Id); } catch (Exception ex) { - Alligator.RouterWriteLine("Receiving Update ({0}) finished with exception {1}", update.Id, ex.Message); + Alligator.LogDebug("Receiving Update ({0}) finished with exception {1}", update.Id, ex.Message); ExceptionHandler?.HandleException(botClient, ex, HandleErrorSource.PollingError, cancellationToken); } } @@ -146,16 +147,16 @@ namespace Telegrator.Polling /// A collection of described handler information for the update protected virtual IEnumerable GetHandlers(IHandlersProvider provider, IUpdateRouter updateRouter, ITelegramBotClient client, Update update, CancellationToken cancellationToken = default) { - Alligator.RouterWriteLine("Requested handlers for UpdateType.{0}", update.Type); + Alligator.LogDebug("Requested handlers for UpdateType.{0}", update.Type); if (!provider.TryGetDescriptorList(update.Type, out HandlerDescriptorList? descriptors)) { - Alligator.RouterWriteLine("No registered, providing Any"); + Alligator.LogDebug("No registered, providing Any"); provider.TryGetDescriptorList(UpdateType.Unknown, out descriptors); } if (descriptors == null || descriptors.Count == 0) { - Alligator.RouterWriteLine("No handlers provided"); + Alligator.LogDebug("No handlers provided"); return []; } @@ -181,7 +182,7 @@ namespace Telegrator.Polling { try { - Alligator.RouterWriteLine("Describing descriptors of descriptorsList.HandlingType.{0} for Update ({1})", descriptors.HandlingType, update.Id); + Alligator.LogDebug("Describing descriptors of descriptorsList.HandlingType.{0} for Update ({1})", descriptors.HandlingType, update.Id); foreach (HandlerDescriptor descriptor in descriptors.Reverse()) { cancellationToken.ThrowIfCancellationRequested(); @@ -199,7 +200,7 @@ namespace Telegrator.Polling } finally { - Alligator.RouterWriteLine("Describing for Update ({0}) finished", update.Id); + Alligator.LogDebug("Describing for Update ({0}) finished", update.Id); } } @@ -253,7 +254,7 @@ namespace Telegrator.Polling if (msg.Sticker != null) sb.AppendFormat(" with sticker '{0}'", msg.Sticker.Emoji); - Alligator.RouterWriteLine(sb.ToString()); + Alligator.LogDebug(sb.ToString()); break; } @@ -268,7 +269,7 @@ namespace Telegrator.Polling if (cq.From != null) sb.AppendFormat(" with data '{0}'", cq.Data); - Alligator.RouterWriteLine(sb.ToString()); + Alligator.LogDebug(sb.ToString()); break; } } diff --git a/Telegrator/Providers/HandlersProvider.cs b/Telegrator/Providers/HandlersProvider.cs index bcc8e80..6e22d42 100644 --- a/Telegrator/Providers/HandlersProvider.cs +++ b/Telegrator/Providers/HandlersProvider.cs @@ -3,6 +3,7 @@ using System.Runtime.CompilerServices; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegrator.Handlers.Components; +using Telegrator.Logging; using Telegrator.MadiatorCore; using Telegrator.MadiatorCore.Descriptors; @@ -40,7 +41,7 @@ namespace Telegrator.Providers AllowedTypes = handlers.AllowedTypes; HandlersDictionary = handlers.Values.ForEach(list => list.Freeze()).ToReadOnlyDictionary(list => list.HandlingType); Options = options ?? throw new ArgumentNullException(nameof(options)); - Alligator.ProviderWriteLine("{0} created!", GetType().Name); + Alligator.LogDebug("{0} created!", GetType().Name); } /// @@ -54,7 +55,7 @@ namespace Telegrator.Providers AllowedTypes = Update.AllTypes; HandlersDictionary = handlers.ForEach(list => list.Freeze()).ToReadOnlyDictionary(list => list.HandlingType); Options = options ?? throw new ArgumentNullException(nameof(options)); - Alligator.ProviderWriteLine("{0} created!", GetType().Name); + Alligator.LogDebug("{0} created!", GetType().Name); } /// @@ -78,7 +79,7 @@ namespace Telegrator.Providers } catch { - Alligator.ProviderWriteLine("Failed to create instance of {0}", descriptor.ToString()); + Alligator.LogDebug("Failed to create instance of {0}", descriptor.ToString()); throw; } } diff --git a/Telegrator/TelegratorClient.cs b/Telegrator/TelegratorClient.cs index 44bb4ce..4a08021 100644 --- a/Telegrator/TelegratorClient.cs +++ b/Telegrator/TelegratorClient.cs @@ -80,7 +80,7 @@ namespace Telegrator updateRouter = new UpdateRouter(handlerProvider, awaitingProvider, Options, BotInfo); // Log startup - Alligator.LogInformation($"Telegrator bot starting up - BotId: {BotInfo.Id}, Username: {BotInfo.Username}, MaxParallelHandlers: {Options.MaximumParallelWorkingHandlers ?? -1}"); + Alligator.LogInformation($"Telegrator bot starting up - BotId: {BotInfo.User.Id}, Username: {BotInfo.User.Username}, MaxParallelHandlers: {Options.MaximumParallelWorkingHandlers ?? -1}"); StartReceivingInternal(receiverOptions, cancellationToken); } diff --git a/Telegrator/TelegratorOptions.cs b/Telegrator/TelegratorOptions.cs index 0787b96..e50b927 100644 --- a/Telegrator/TelegratorOptions.cs +++ b/Telegrator/TelegratorOptions.cs @@ -8,14 +8,6 @@ namespace Telegrator /// public class TelegratorOptions : ITelegratorOptions { - /* - /// - public bool ExecuteOnlyFirstFoundHanlder { get; set; } - - /// - public bool DescendDescriptorIndex { get; set; } = true; - */ - /// public int? MaximumParallelWorkingHandlers { get; set; }