From 08ebdbaa37d2e6412aaae76bbfbb7cabef29007f Mon Sep 17 00:00:00 2001 From: Nikolay Kochulin Date: Wed, 6 Feb 2019 18:19:09 +0300 Subject: [PATCH] Make logging accessible via perfusion --- JetKarmaBot/CommandRouter.cs | 4 +++- JetKarmaBot/Commands/AwardCommand.cs | 3 ++- JetKarmaBot/Commands/ChangeLocaleCommand.cs | 3 ++- JetKarmaBot/JetKarmaBot.cs | 2 +- JetKarmaBot/Program.cs | 1 + JetKarmaBot/Services/Localization.cs | 6 ++++-- JetKarmaBot/Services/LogInfo.cs | 20 ++++++++++++++++++++ perfusion | 2 +- 8 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 JetKarmaBot/Services/LogInfo.cs diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index b4efdc5..53edf69 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -1,5 +1,6 @@ using JetKarmaBot.Commands; using NLog; +using Perfusion; using System; using System.Collections.Generic; using System.Linq; @@ -11,7 +12,8 @@ namespace JetKarmaBot class ChatCommandRouter { User BotUser { get; } - private static Logger log = LogManager.GetCurrentClassLogger(); + [Inject] + private Logger log; public ChatCommandRouter(User botUser) { diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index a3a0a9c..6d07a51 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -13,7 +13,8 @@ namespace JetKarmaBot.Commands class AwardCommand : IChatCommand { public IReadOnlyCollection Names => new[] { "award", "revoke" }; - private static Logger log = LogManager.GetCurrentClassLogger(); + [Inject] + private Logger log; public bool Execute(CommandString cmd, MessageEventArgs args) { diff --git a/JetKarmaBot/Commands/ChangeLocaleCommand.cs b/JetKarmaBot/Commands/ChangeLocaleCommand.cs index a876385..c2290e9 100644 --- a/JetKarmaBot/Commands/ChangeLocaleCommand.cs +++ b/JetKarmaBot/Commands/ChangeLocaleCommand.cs @@ -10,7 +10,8 @@ namespace JetKarmaBot.Commands class LocaleCommand : IChatCommand { public IReadOnlyCollection Names => new[] { "changelocale", "locale" }; - private static Logger log = LogManager.GetCurrentClassLogger(); + [Inject] + private Logger log; public bool Execute(CommandString cmd, MessageEventArgs args) { diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 8f84b41..642bb4f 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -72,7 +72,7 @@ namespace JetKarmaBot void InitCommands(Container c) { - Commands = new ChatCommandRouter(Me); + Commands = c.ResolveObject(new ChatCommandRouter(Me)); Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new StatusCommand())); diff --git a/JetKarmaBot/Program.cs b/JetKarmaBot/Program.cs index 74817e7..5605a52 100644 --- a/JetKarmaBot/Program.cs +++ b/JetKarmaBot/Program.cs @@ -29,6 +29,7 @@ namespace JetKarmaBot var dbOptions = new DbContextOptionsBuilder() .UseMySql(cfg.ConnectionString); + c.AddInfo(new LogInfo()); c.AddTransient(() => new KarmaContext(dbOptions.Options)); c.Add(); diff --git a/JetKarmaBot/Services/Localization.cs b/JetKarmaBot/Services/Localization.cs index b93163a..adbb1ac 100644 --- a/JetKarmaBot/Services/Localization.cs +++ b/JetKarmaBot/Services/Localization.cs @@ -13,8 +13,9 @@ namespace JetKarmaBot private Dictionary locales = new Dictionary(); [Inject] - public Localization() + public Localization(Container c) { + c.ResolveObject(this); log.Info("Initializing..."); string langsFolder = "lang"; if (!Directory.Exists(langsFolder)) @@ -51,7 +52,8 @@ namespace JetKarmaBot } } - private static Logger log = LogManager.GetCurrentClassLogger(); + [Inject] + private Logger log; public Locale FindByCommonName(string name) { diff --git a/JetKarmaBot/Services/LogInfo.cs b/JetKarmaBot/Services/LogInfo.cs new file mode 100644 index 0000000..b2729df --- /dev/null +++ b/JetKarmaBot/Services/LogInfo.cs @@ -0,0 +1,20 @@ +using System; +using System.Linq; +using NLog; +using Perfusion; + +namespace JetKarmaBot +{ + public class LogInfo : ObjectInfo + { + public override object GetInstance(Type requester = null) + { + return LogManager.GetLogger(requester != null ? getTypeName(requester) : ""); + } + private string getTypeName(Type t) + { + return (t.DeclaringType == null ? t.Namespace + "." + t.Name : getTypeName(t.DeclaringType) + "." + t.Name) + + (t.GenericTypeArguments.Length > 0 ? "<" + string.Join(",", t.GenericTypeArguments.Select(getTypeName)) + ">" : ""); + } + } +} \ No newline at end of file diff --git a/perfusion b/perfusion index a806f4d..a9503a6 160000 --- a/perfusion +++ b/perfusion @@ -1 +1 @@ -Subproject commit a806f4d9fb9eb0e3036567304cabe1adf65d476c +Subproject commit a9503a6b2cfa9166d4904ad995bc81d39ec9b2b7