Add exception logging

This commit is contained in:
Noikoio 2020-05-06 17:55:53 -07:00
parent ce1452b6ed
commit 67b89f6bc2

View file

@ -14,6 +14,7 @@ namespace Noikoio.RegexBot
private static Configuration _config; private static Configuration _config;
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private BotModule[] _modules; private BotModule[] _modules;
private readonly AsyncLogger _dlog;
internal static Configuration Config => _config; internal static Configuration Config => _config;
internal IEnumerable<BotModule> Modules => _modules; internal IEnumerable<BotModule> Modules => _modules;
@ -48,7 +49,7 @@ namespace Noikoio.RegexBot
}); });
// Hook up basic handlers and other references // Hook up basic handlers and other references
_client.Connected += _client_Connected; _client.Connected += Client_Connected;
EntityCache.EntityCache.SetClient(_client); EntityCache.EntityCache.SetClient(_client);
// Initialize modules // Initialize modules
@ -68,11 +69,8 @@ namespace Noikoio.RegexBot
}; };
// Set up logging // Set up logging
var dlog = Logger.GetLogger("Discord.Net"); _dlog = Logger.GetLogger("Discord.Net");
_client.Log += async (arg) => _client.Log += Client_Log;
await dlog(
String.Format("{0}: {1}{2}", arg.Source, ((int)arg.Severity < 3 ? arg.Severity + ": " : ""),
arg.Message));
// Finish loading configuration // Finish loading configuration
var conf = _config.ReloadServerConfig().Result; var conf = _config.ReloadServerConfig().Result;
@ -96,7 +94,7 @@ namespace Noikoio.RegexBot
await Task.Delay(-1); 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 // Defined within this class because a reference to the client is required
public void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) public void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
@ -111,5 +109,13 @@ namespace Noikoio.RegexBot
#endif #endif
Environment.Exit(0); 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());
}
} }
} }