Make logging accessible via perfusion

This commit is contained in:
Basique Evangelist 2019-02-06 18:19:09 +03:00
parent b5b5478446
commit 89a0408ae5
8 changed files with 34 additions and 7 deletions

View File

@ -1,5 +1,6 @@
using JetKarmaBot.Commands; using JetKarmaBot.Commands;
using NLog; using NLog;
using Perfusion;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,7 +12,8 @@ namespace JetKarmaBot
class ChatCommandRouter class ChatCommandRouter
{ {
User BotUser { get; } User BotUser { get; }
private static Logger log = LogManager.GetCurrentClassLogger(); [Inject]
private Logger log;
public ChatCommandRouter(User botUser) public ChatCommandRouter(User botUser)
{ {

View File

@ -13,7 +13,8 @@ namespace JetKarmaBot.Commands
class AwardCommand : IChatCommand class AwardCommand : IChatCommand
{ {
public IReadOnlyCollection<string> Names => new[] { "award", "revoke" }; public IReadOnlyCollection<string> Names => new[] { "award", "revoke" };
private static Logger log = LogManager.GetCurrentClassLogger(); [Inject]
private Logger log;
public bool Execute(CommandString cmd, MessageEventArgs args) public bool Execute(CommandString cmd, MessageEventArgs args)
{ {

View File

@ -10,7 +10,8 @@ namespace JetKarmaBot.Commands
class LocaleCommand : IChatCommand class LocaleCommand : IChatCommand
{ {
public IReadOnlyCollection<string> Names => new[] { "changelocale", "locale" }; public IReadOnlyCollection<string> Names => new[] { "changelocale", "locale" };
private static Logger log = LogManager.GetCurrentClassLogger(); [Inject]
private Logger log;
public bool Execute(CommandString cmd, MessageEventArgs args) public bool Execute(CommandString cmd, MessageEventArgs args)
{ {

View File

@ -72,7 +72,7 @@ namespace JetKarmaBot
void InitCommands(Container c) 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 StartCommand()));
Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new AwardCommand(Me)));
Commands.Add(c.ResolveObject(new StatusCommand())); Commands.Add(c.ResolveObject(new StatusCommand()));

View File

@ -29,6 +29,7 @@ namespace JetKarmaBot
var dbOptions = new DbContextOptionsBuilder<KarmaContext>() var dbOptions = new DbContextOptionsBuilder<KarmaContext>()
.UseMySql(cfg.ConnectionString); .UseMySql(cfg.ConnectionString);
c.AddInfo<Logger>(new LogInfo());
c.AddTransient(() => new KarmaContext(dbOptions.Options)); c.AddTransient(() => new KarmaContext(dbOptions.Options));
c.Add<JetKarmaBot>(); c.Add<JetKarmaBot>();

View File

@ -13,8 +13,9 @@ namespace JetKarmaBot
private Dictionary<string, Locale> locales = new Dictionary<string, Locale>(); private Dictionary<string, Locale> locales = new Dictionary<string, Locale>();
[Inject] [Inject]
public Localization() public Localization(Container c)
{ {
c.ResolveObject(this);
log.Info("Initializing..."); log.Info("Initializing...");
string langsFolder = "lang"; string langsFolder = "lang";
if (!Directory.Exists(langsFolder)) 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) public Locale FindByCommonName(string name)
{ {

View File

@ -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) : "<type unspecified>");
}
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)) + ">" : "");
}
}
}

@ -1 +1 @@
Subproject commit a806f4d9fb9eb0e3036567304cabe1adf65d476c Subproject commit a9503a6b2cfa9166d4904ad995bc81d39ec9b2b7