diff --git a/DiscordBot/Bot/Actions/Exit.cs b/DiscordBot/Bot/Actions/Exit.cs index 4451e75..92ffcb8 100644 --- a/DiscordBot/Bot/Actions/Exit.cs +++ b/DiscordBot/Bot/Actions/Exit.cs @@ -24,7 +24,7 @@ public class Exit: ICommandAction { if (args is null || args.Length == 0) { - Application.CurrentApplication.Logger.Log("Exiting...", typeof(ICommandAction), LogType.WARNING); + Application.CurrentApplication.Logger.Log("Exiting...", this, LogType.WARNING); await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile(); Environment.Exit(0); } @@ -40,7 +40,7 @@ public class Exit: ICommandAction case "-f": case "force": - Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", typeof(ICommandAction), LogType.WARNING); + Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", this, LogType.WARNING); Environment.Exit(0); break; diff --git a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs index 1dbb084..64939ed 100644 --- a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs +++ b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -148,15 +149,13 @@ internal static class PluginMethods { if (data.IsSuccess) { - Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, typeof(ICommandAction), - LogType.INFO - ); + Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, LogType.INFO, "\t\t > {Message}"); } else { Application.CurrentApplication.Logger.Log("Failed to load command : " + data.PluginName + " because " + data.ErrorMessage, - typeof(ICommandAction), LogType.ERROR + typeof(PluginMethods), LogType.ERROR ); } @@ -166,14 +165,12 @@ internal static class PluginMethods { if (data.IsSuccess) { - Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, typeof(ICommandAction), - LogType.INFO - ); + Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, LogType.INFO, "\t\t > {Message}"); } else { Application.CurrentApplication.Logger.Log("Failed to load event : " + data.PluginName + " because " + data.ErrorMessage, - typeof(ICommandAction), LogType.ERROR + typeof(PluginMethods), LogType.ERROR ); } @@ -184,14 +181,12 @@ internal static class PluginMethods { if (data.IsSuccess) { - Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, typeof(ICommandAction), - LogType.INFO - ); + Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, LogType.INFO, "\t\t > {Message}"); } else { Application.CurrentApplication.Logger.Log("Failed to load slash command : " + data.PluginName + " because " + data.ErrorMessage, - typeof(ICommandAction), LogType.ERROR + typeof(PluginMethods), LogType.ERROR ); } diff --git a/DiscordBot/Bot/Actions/Plugin.cs b/DiscordBot/Bot/Actions/Plugin.cs index 85deb78..e397d42 100644 --- a/DiscordBot/Bot/Actions/Plugin.cs +++ b/DiscordBot/Bot/Actions/Plugin.cs @@ -61,13 +61,13 @@ public class Plugin: ICommandAction case "load": if (pluginsLoaded) { - Application.CurrentApplication.Logger.Log("Plugins already loaded", typeof(ICommandAction), LogType.WARNING); + Application.CurrentApplication.Logger.Log("Plugins already loaded", this, LogType.WARNING); break; } if (Application.CurrentApplication.DiscordBotClient is null) { - Application.CurrentApplication.Logger.Log("DiscordBot is null", typeof(ICommandAction), LogType.WARNING); + Application.CurrentApplication.Logger.Log("DiscordBot is null", this, LogType.WARNING); break; } diff --git a/DiscordBotCore/Bot/Boot.cs b/DiscordBotCore/Bot/Boot.cs index db1d074..70f3dfd 100644 --- a/DiscordBotCore/Bot/Boot.cs +++ b/DiscordBotCore/Bot/Boot.cs @@ -95,7 +95,6 @@ public class Boot private void CommonTasks() { if (Client == null) return; - Client.LoggedOut += Client_LoggedOut; Client.Log += Log; Client.LoggedIn += LoggedIn; Client.Ready += Ready; @@ -107,18 +106,12 @@ public class Boot if (arg.Message.Contains("401")) { Application.CurrentApplication.ApplicationEnvironmentVariables.Remove("token"); - Application.CurrentApplication.Logger.Log("The token is invalid. Please restart the bot and follow the instructions", typeof(Boot), LogType.CRITICAL); + Application.CurrentApplication.Logger.Log("The token is invalid. Please restart the bot and follow the instructions", this, LogType.CRITICAL); await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile(); Environment.Exit(0); } } - private async Task Client_LoggedOut() - { - Application.CurrentApplication.Logger.Log("Successfully Logged Out", typeof(Boot)); - await Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !")); - } - private Task Ready() { IsReady = true; @@ -127,7 +120,7 @@ public class Boot private Task LoggedIn() { - Application.CurrentApplication.Logger.Log("Successfully Logged In", typeof(Boot)); + Application.CurrentApplication.Logger.Log("Successfully Logged In", this); return Task.CompletedTask; } @@ -137,12 +130,12 @@ public class Boot { case LogSeverity.Error: case LogSeverity.Critical: - Application.CurrentApplication.Logger.Log(message.Message, typeof(Boot), LogType.ERROR); + Application.CurrentApplication.Logger.Log(message.Message, this, LogType.ERROR); break; case LogSeverity.Info: case LogSeverity.Debug: - Application.CurrentApplication.Logger.Log(message.Message, typeof(Boot), LogType.INFO); + Application.CurrentApplication.Logger.Log(message.Message, this, LogType.INFO); break; diff --git a/DiscordBotCore/Bot/CommandHandler.cs b/DiscordBotCore/Bot/CommandHandler.cs index 6b95827..b67e657 100644 --- a/DiscordBotCore/Bot/CommandHandler.cs +++ b/DiscordBotCore/Bot/CommandHandler.cs @@ -142,7 +142,7 @@ internal class CommandHandler Application.CurrentApplication.Logger.Log( $"User ({context.User.Username}) from Guild \"{context.Guild.Name}\" executed command \"{cmd.cleanContent}\"", - typeof(CommandHandler), + this, LogType.INFO ); diff --git a/DiscordBotCore/Loaders/PluginLoader.cs b/DiscordBotCore/Loaders/PluginLoader.cs index a2dca4a..edabc77 100644 --- a/DiscordBotCore/Loaders/PluginLoader.cs +++ b/DiscordBotCore/Loaders/PluginLoader.cs @@ -32,7 +32,7 @@ public class PluginLoader public async Task LoadPlugins() { - Application.CurrentApplication.Logger.Log("Loading plugins...", typeof(PluginLoader)); + Application.CurrentApplication.Logger.Log("Loading plugins...", this); var loader = new Loader(Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"], "dll"); @@ -46,7 +46,7 @@ public class PluginLoader private void FileLoadedException(FileLoaderResult result) { - Application.CurrentApplication.Logger.Log(result.ErrorMessage, typeof(PluginLoader), LogType.ERROR); + Application.CurrentApplication.Logger.Log(result.ErrorMessage, this, LogType.ERROR); } private async void OnPluginLoaded(PluginLoadResultData result) @@ -74,11 +74,11 @@ public class PluginLoader OnSlashCommandLoaded?.Invoke(result); } else - Application.CurrentApplication.Logger.Log($"Failed to start slash command {result.PluginName}", typeof(PluginLoader), LogType.ERROR); + Application.CurrentApplication.Logger.Log($"Failed to start slash command {result.PluginName}", this, LogType.ERROR); break; case PluginType.UNKNOWN: default: - Application.CurrentApplication.Logger.Log("Unknown plugin type", typeof(PluginLoader), LogType.ERROR); + Application.CurrentApplication.Logger.Log("Unknown plugin type", this, LogType.ERROR); break; } } diff --git a/DiscordBotCore/Online/PluginManager.cs b/DiscordBotCore/Online/PluginManager.cs index 1aa31c0..34fab64 100644 --- a/DiscordBotCore/Online/PluginManager.cs +++ b/DiscordBotCore/Online/PluginManager.cs @@ -101,7 +101,7 @@ public class PluginManager { if (await pluginUpdater.HasUpdate(plugin.PluginName)) { - Application.CurrentApplication.Logger.Log("Updating plugin: " + plugin.PluginName, typeof(PluginManager), LogType.INFO); + Application.CurrentApplication.Logger.Log("Updating plugin: " + plugin.PluginName, this, LogType.INFO); await pluginUpdater.UpdatePlugin(plugin.PluginName); } } diff --git a/DiscordBotCore/Others/Logger/LogMessage.cs b/DiscordBotCore/Others/Logger/LogMessage.cs index 92b069a..b369344 100644 --- a/DiscordBotCore/Others/Logger/LogMessage.cs +++ b/DiscordBotCore/Others/Logger/LogMessage.cs @@ -11,6 +11,14 @@ namespace DiscordBotCore.Others.Logger public string SenderName { get; set; } public LogType LogMessageType { get; set; } + public LogMessage(string message, LogType logMessageType) + { + Message = message; + LogMessageType = logMessageType; + ThrowTime = DateTime.Now; + SenderName = string.Empty; + } + public LogMessage(string message, object sender) { Message = message; diff --git a/DiscordBotCore/Others/Logger/Logger.cs b/DiscordBotCore/Others/Logger/Logger.cs index 7e0154e..6a079a1 100644 --- a/DiscordBotCore/Others/Logger/Logger.cs +++ b/DiscordBotCore/Others/Logger/Logger.cs @@ -37,6 +37,25 @@ public sealed class Logger : ILogger return messageAsString; } + private string GenerateLogMessage(ILogMessage message, string customFormat) + { + string messageAsString = customFormat; + foreach (var prop in LogMessageProperties) + { + Type messageType = typeof(ILogMessage); + messageAsString = messageAsString.Replace("{" + prop + "}", messageType?.GetProperty(prop)?.GetValue(message)?.ToString()); + } + + return messageAsString; + } + + public void Log(ILogMessage message, string format) + { + OnRawLog?.Invoke(this, message); + string messageAsString = GenerateLogMessage(message, format); + OnFormattedLog?.Invoke(this, new ILogger.FormattedMessage() { Message = messageAsString, Type = message.LogMessageType }); + } + public void Log(ILogMessage message) { OnRawLog?.Invoke(this, message); @@ -44,6 +63,8 @@ public sealed class Logger : ILogger OnFormattedLog?.Invoke(this, new ILogger.FormattedMessage() { Message = messageAsString, Type = message.LogMessageType }) ; } + public void Log(string message, LogType logType, string format) => Log(new LogMessage(message, logType), format); + public void Log(string message, LogType logType) => Log(new LogMessage(message, logType)); public void Log(string message, object Sender) => Log(new LogMessage(message, Sender)); public void Log(string message, object Sender, LogType type) => Log(new LogMessage(message, Sender, type)); public void LogException(Exception exception, object Sender) => Log(LogMessage.CreateFromException(exception, Sender)); diff --git a/DiscordBotUI/DiscordBotUI/Views/SettingsPage.axaml.cs b/DiscordBotUI/DiscordBotUI/Views/SettingsPage.axaml.cs index 604ef0b..6fea35e 100644 --- a/DiscordBotUI/DiscordBotUI/Views/SettingsPage.axaml.cs +++ b/DiscordBotUI/DiscordBotUI/Views/SettingsPage.axaml.cs @@ -1,7 +1,5 @@ using Avalonia.Controls; -using DiscordBotCore; - namespace DiscordBotUI.Views; public partial class SettingsPage : Window