From cc82c02e44c6a920b56cf98a6e2e3a218ed7b401 Mon Sep 17 00:00:00 2001 From: Basique Evangelist Date: Sun, 6 Jan 2019 23:39:31 +0300 Subject: [PATCH] Add logging to Program and CommandRouter --- JetKarmaBot/CommandRouter.cs | 18 +++++++++++++++--- JetKarmaBot/Program.cs | 14 ++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index 9cebeb4..b4efdc5 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -1,4 +1,5 @@ using JetKarmaBot.Commands; +using NLog; using System; using System.Collections.Generic; using System.Linq; @@ -10,6 +11,7 @@ namespace JetKarmaBot class ChatCommandRouter { User BotUser { get; } + private static Logger log = LogManager.GetCurrentClassLogger(); public ChatCommandRouter(User botUser) { @@ -18,21 +20,29 @@ namespace JetKarmaBot public bool Execute(object sender, MessageEventArgs args) { + log.Debug("Message received"); var text = args.Message.Text; - if (CommandString.TryParse(text, out var cmd)) { - if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us! + if (cmd.UserName != null && cmd.UserName != BotUser.Username) + { + // directed not at us! + log.Debug("Message not directed at us"); return false; + } try { if (commands.ContainsKey(cmd.Command)) + { + log.Debug($"Handling message via {commands[cmd.Command].GetType().Name}"); return commands[cmd.Command].Execute(cmd, args); + } } catch (Exception e) { - Console.WriteLine(e); + log.Error($"Error while handling command {cmd.Command}!"); + log.Error(e); } } @@ -42,8 +52,10 @@ namespace JetKarmaBot public void Add(IChatCommand c) { + log.ConditionalTrace($"Adding command {c.GetType().Name}"); foreach (var name in c.Names) { + log.ConditionalTrace($"Mounting {c.GetType().Name} to {name}"); if (commands.ContainsKey(name)) throw new Exception($"command collision for name {name}, commands {commands[name].GetType()} and {c.GetType()}"); commands[name] = c; diff --git a/JetKarmaBot/Program.cs b/JetKarmaBot/Program.cs index 4981083..329369f 100644 --- a/JetKarmaBot/Program.cs +++ b/JetKarmaBot/Program.cs @@ -2,12 +2,14 @@ using System.Threading; using JetKarmaBot.Models; using Microsoft.EntityFrameworkCore; +using NLog; using Perfusion; namespace JetKarmaBot { public static class Program { + private static Logger log = LogManager.GetCurrentClassLogger(); public enum ExitCode : int { Ok = 0, @@ -17,13 +19,9 @@ namespace JetKarmaBot ErrorInvalidCommandLine = 0x100 }; -#if DEBUG - public const bool Debug = true; -#else - public const bool Debug = false; -#endif public static int Main(string[] args) { + log.Info("Starting JetKarmaBot."); Container c = new Container(); var cfg = new Config("karma.cfg.json"); c.AddInstance(cfg); @@ -39,12 +37,12 @@ namespace JetKarmaBot try { bot.Init().Wait(); - Console.WriteLine("JetKarmaBot started. Press Ctrl-C to exit..."); + log.Info("JetKarmaBot started. Press Ctrl-C to exit..."); Environment.ExitCode = (int)ExitCode.ErrorRunning; } catch (Exception ex) { - Console.WriteLine($"Exception: {ex.Message}"); + log.Error(ex); Environment.ExitCode = (int)ExitCode.ErrorException; } ManualResetEvent quitEvent = new ManualResetEvent(false); @@ -59,7 +57,7 @@ namespace JetKarmaBot catch { } quitEvent.WaitOne(Timeout.Infinite); - Console.WriteLine("Waiting for exit..."); + log.Info("Waiting for exit..."); bot?.Stop()?.Wait(); return (int)ExitCode.Ok;