Added logging

This commit is contained in:
Basique Evangelist 2019-01-06 23:24:52 +03:00
parent 7820d50fe8
commit 66190a1eb0
3 changed files with 37 additions and 18 deletions

View File

@ -1,21 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" />
<PackageReference Include="Telegram.Bot" Version="14.10.0" />
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4"/>
<PackageReference Include="Telegram.Bot" Version="14.10.0"/>
<PackageReference Include="NLog" Version="5.0.0-beta11"/>
<PackageReference Include="NLog.Config" Version="4.5.11"/>
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj"/>
</ItemGroup>
<ItemGroup>
<None Update="karma.cfg.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@ -26,6 +25,8 @@
<None Update="lang\ru-RU.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

14
JetKarmaBot/NLog.config Executable file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="karmabot.log" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Trace" writeTo="logfile" />
</rules>
</nlog>

View File

@ -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<string, Locale> locales = new Dictionary<string, Locale>();
[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