From 66190a1eb033d914b5df8113944b38bf83148ad3 Mon Sep 17 00:00:00 2001 From: Basique Evangelist Date: Sun, 6 Jan 2019 23:24:52 +0300 Subject: [PATCH] Added logging --- JetKarmaBot/JetKarmaBot.csproj | 19 ++++++++++--------- JetKarmaBot/NLog.config | 14 ++++++++++++++ JetKarmaBot/Services/Localization.cs | 22 +++++++++++++--------- 3 files changed, 37 insertions(+), 18 deletions(-) create mode 100755 JetKarmaBot/NLog.config diff --git a/JetKarmaBot/JetKarmaBot.csproj b/JetKarmaBot/JetKarmaBot.csproj index f2f5fd2..8641b19 100644 --- a/JetKarmaBot/JetKarmaBot.csproj +++ b/JetKarmaBot/JetKarmaBot.csproj @@ -1,21 +1,20 @@ - Exe netcoreapp2.1 - all runtime; build; native; contentfiles; analyzers - - - - + + + + + + - Always @@ -26,6 +25,8 @@ Always + + Always + - - + \ No newline at end of file diff --git a/JetKarmaBot/NLog.config b/JetKarmaBot/NLog.config new file mode 100755 index 0000000..a86bbe4 --- /dev/null +++ b/JetKarmaBot/NLog.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + diff --git a/JetKarmaBot/Services/Localization.cs b/JetKarmaBot/Services/Localization.cs index a8ce96d..b93163a 100644 --- a/JetKarmaBot/Services/Localization.cs +++ b/JetKarmaBot/Services/Localization.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using Newtonsoft.Json.Linq; +using NLog; using Perfusion; namespace JetKarmaBot @@ -12,9 +13,9 @@ namespace JetKarmaBot private Dictionary locales = new Dictionary(); [Inject] - public Localization(Config cfg) + public Localization() { - Log("Initializing..."); + log.Info("Initializing..."); string langsFolder = "lang"; if (!Directory.Exists(langsFolder)) Directory.CreateDirectory(langsFolder); @@ -26,17 +27,17 @@ namespace JetKarmaBot string langName = Path.GetFileNameWithoutExtension(langFilePath); string langKey = langName.ToLowerInvariant(); locales[langKey] = new Locale(JObject.Parse(File.ReadAllText(langFilePath)), langKey); - Log("Found " + langName); + log.Debug("Found " + langName); } catch (Exception e) { - Log($"Error while parsing {langFilePath}!"); - Log(e); + log.Error($"Error while parsing {langFilePath}!"); + log.Error(e); } } if (locales.Any()) - Log("Initialized!"); + log.Info("Initialized!"); else throw new FileNotFoundException($"No locales found in {langsFolder}!"); } @@ -49,15 +50,21 @@ namespace JetKarmaBot return locales[locale]; } } + + private static Logger log = LogManager.GetCurrentClassLogger(); + public Locale FindByCommonName(string name) { + log.ConditionalTrace("Trying to find locale " + name); foreach (Locale l in locales.Values) { if (l.CommonNames.Contains(name)) { + log.ConditionalTrace("Found locale " + l.Name); return l; } } + log.Warn("Failed to find locale " + name); return null; } public bool ContainsLocale(string locale) @@ -66,9 +73,6 @@ namespace JetKarmaBot return locales.ContainsKey(locale); } - void Log(string Message) - => Console.WriteLine($"[{nameof(Localization)}]: {Message}"); - void Log(Exception e) => Console.WriteLine(e); public class Locale