From fa7e7988d5d418d0dee9b1109f28bd6a114a0930 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Sat, 29 Jun 2024 22:32:37 +0300 Subject: [PATCH] Added Filelogging --- DiscordBot/Program.cs | 10 +++++++--- DiscordBotCore/Others/Logger/Logger.cs | 13 ++++++------- SethDiscordBot.sln | 23 ----------------------- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 2130b8b..2e11d54 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -66,8 +66,8 @@ public class Program try { - var token = Application.CurrentApplication.ApplicationEnvironmentVariables["token"]; - var prefix = Application.CurrentApplication.ApplicationEnvironmentVariables["prefix"]; + var token = Application.CurrentApplication.ApplicationEnvironmentVariables["token"]; + var prefix = Application.CurrentApplication.ApplicationEnvironmentVariables["prefix"]; var discordbooter = new App(token, prefix); await discordbooter.Awake(); } @@ -77,6 +77,10 @@ public class Program } } + /// + /// Load the bot components. + /// + /// The startup arguments private static async Task LoadComponents(string[] args) { await Application.CreateApplication(); @@ -118,7 +122,7 @@ public class Program if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") || !Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") || !Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix")) - await Installer.GenerateStartupConfig(); + await Installer.GenerateStartupConfig(); } diff --git a/DiscordBotCore/Others/Logger/Logger.cs b/DiscordBotCore/Others/Logger/Logger.cs index 814f133..79eb79f 100644 --- a/DiscordBotCore/Others/Logger/Logger.cs +++ b/DiscordBotCore/Others/Logger/Logger.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Threading.Tasks; using DiscordBotCore.Interfaces.Logger; @@ -10,7 +9,7 @@ namespace DiscordBotCore.Others.Logger; public sealed class Logger : ILogger { - private readonly FileStream _LogStream; + private readonly FileStream _LogFileStream; public List LogMessageProperties = typeof(ILogMessage).GetProperties().Select(p => p.Name).ToList(); public string LogMessageFormat { get ; set; } @@ -21,7 +20,7 @@ public sealed class Logger : ILogger public Logger(string logMessageFormat) { this.LogMessageFormat = logMessageFormat; - _LogStream = File.Open(Application.CurrentApplication.LogFile, FileMode.Append, FileAccess.Write, FileShare.Read); + _LogFileStream = File.Open(Application.CurrentApplication.LogFile, FileMode.Append, FileAccess.Write, FileShare.Read); } /// @@ -44,12 +43,12 @@ public sealed class Logger : ILogger private async void LogToFile(string message) { byte[] messageAsBytes = System.Text.Encoding.ASCII.GetBytes(message); - await _LogStream.WriteAsync(messageAsBytes, 0, messageAsBytes.Length); + await _LogFileStream.WriteAsync(messageAsBytes, 0, messageAsBytes.Length); byte[] newLine = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine); - await _LogStream.WriteAsync(newLine, 0, newLine.Length); + await _LogFileStream.WriteAsync(newLine, 0, newLine.Length); - await _LogStream.FlushAsync(); + await _LogFileStream.FlushAsync(); } private string GenerateLogMessage(ILogMessage message, string customFormat) @@ -78,6 +77,7 @@ public sealed class Logger : ILogger string messageAsString = GenerateLogMessage(message); OnFormattedLog?.Invoke(this, new ILogger.FormattedMessage() { Message = messageAsString, Type = message.LogMessageType }) ; LogToFile(messageAsString); + } public void Log(string message) => Log(new LogMessage(message, string.Empty, LogType.INFO)); @@ -86,5 +86,4 @@ public sealed class Logger : ILogger 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/SethDiscordBot.sln b/SethDiscordBot.sln index c4b9785..642b47b 100644 --- a/SethDiscordBot.sln +++ b/SethDiscordBot.sln @@ -8,14 +8,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBotCore", "DiscordBo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBotUI_Windows", "DiscordBotUI\DiscordBotUI_Windows.csproj", "{95AD2D0C-D1EB-47B9-8DB4-F11CBAF15704}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugins", "Plugins", "{8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicPlayer", "..\SethPlugins\MusicPlayer\MusicPlayer.csproj", "{F094D29D-6F1A-46BC-8B1F-88F3D98FCA84}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LevelingSystem", "..\SethPlugins\LevelingSystem\LevelingSystem.csproj", "{39EF14F8-7E67-4C14-A4F1-E706CFF61192}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CppWrapper", "..\SethPlugins\CppWrapper\CppWrapper.csproj", "{2E526F88-368C-4BE5-9B46-69EE3D311E9B}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -34,26 +26,11 @@ Global {95AD2D0C-D1EB-47B9-8DB4-F11CBAF15704}.Debug|Any CPU.Build.0 = Debug|Any CPU {95AD2D0C-D1EB-47B9-8DB4-F11CBAF15704}.Release|Any CPU.ActiveCfg = Release|Any CPU {95AD2D0C-D1EB-47B9-8DB4-F11CBAF15704}.Release|Any CPU.Build.0 = Release|Any CPU - {F094D29D-6F1A-46BC-8B1F-88F3D98FCA84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F094D29D-6F1A-46BC-8B1F-88F3D98FCA84}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F094D29D-6F1A-46BC-8B1F-88F3D98FCA84}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F094D29D-6F1A-46BC-8B1F-88F3D98FCA84}.Release|Any CPU.Build.0 = Release|Any CPU - {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Debug|Any CPU.Build.0 = Debug|Any CPU - {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Release|Any CPU.ActiveCfg = Release|Any CPU - {39EF14F8-7E67-4C14-A4F1-E706CFF61192}.Release|Any CPU.Build.0 = Release|Any CPU - {2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2E526F88-368C-4BE5-9B46-69EE3D311E9B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {F094D29D-6F1A-46BC-8B1F-88F3D98FCA84} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC} - {39EF14F8-7E67-4C14-A4F1-E706CFF61192} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC} - {2E526F88-368C-4BE5-9B46-69EE3D311E9B} = {8FAEFF5E-F749-435C-BB7B-D57C0F8B17FC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3FB3C5DE-ED21-4D2E-ABDD-3A00EE4A2FFF}