From 67b89f6bc2099957937f48798c577ea1d6c64e87 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Wed, 6 May 2020 17:55:53 -0700 Subject: [PATCH] Add exception logging --- RegexBot/RegexBot.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/RegexBot/RegexBot.cs b/RegexBot/RegexBot.cs index 7a7b525..8dcea41 100644 --- a/RegexBot/RegexBot.cs +++ b/RegexBot/RegexBot.cs @@ -14,6 +14,7 @@ namespace Noikoio.RegexBot private static Configuration _config; private readonly DiscordSocketClient _client; private BotModule[] _modules; + private readonly AsyncLogger _dlog; internal static Configuration Config => _config; internal IEnumerable Modules => _modules; @@ -48,7 +49,7 @@ namespace Noikoio.RegexBot }); // Hook up basic handlers and other references - _client.Connected += _client_Connected; + _client.Connected += Client_Connected; EntityCache.EntityCache.SetClient(_client); // Initialize modules @@ -68,11 +69,8 @@ namespace Noikoio.RegexBot }; // Set up logging - var dlog = Logger.GetLogger("Discord.Net"); - _client.Log += async (arg) => - await dlog( - String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""), - arg.Message)); + _dlog = Logger.GetLogger("Discord.Net"); + _client.Log += Client_Log; // Finish loading configuration var conf = _config.ReloadServerConfig().Result; @@ -96,7 +94,7 @@ namespace Noikoio.RegexBot await Task.Delay(-1); } - private async Task _client_Connected() => await _client.SetGameAsync(Config.CurrentGame); + private async Task Client_Connected() => await _client.SetGameAsync(Config.CurrentGame); // Defined within this class because a reference to the client is required public void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) @@ -111,5 +109,13 @@ namespace Noikoio.RegexBot #endif Environment.Exit(0); } + + public async Task Client_Log(LogMessage arg) + { + await _dlog( + String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""), + arg.Message)); + if (arg.Exception != null) await _dlog(arg.Exception.ToString()); + } } }