Added logging

This commit is contained in:
Nikolay Kochulin 2019-01-06 23:24:52 +03:00
parent 51dce7f588
commit a9cca3ee59
3 changed files with 37 additions and 18 deletions

View File

@ -1,21 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.1"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4"/>
<PackageReference Include="Telegram.Bot" Version="14.10.0" /> <PackageReference Include="Telegram.Bot" Version="14.10.0"/>
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj" /> <PackageReference Include="NLog" Version="5.0.0-beta11"/>
<PackageReference Include="NLog.Config" Version="4.5.11"/>
<ProjectReference Include="..\perfusion\Perfusion\Perfusion.csproj"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="karma.cfg.json"> <None Update="karma.cfg.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
@ -26,6 +25,8 @@
<None Update="lang\ru-RU.json"> <None Update="lang\ru-RU.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </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.IO;
using System.Linq; using System.Linq;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NLog;
using Perfusion; using Perfusion;
namespace JetKarmaBot namespace JetKarmaBot
@ -12,9 +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(Config cfg) public Localization()
{ {
Log("Initializing..."); log.Info("Initializing...");
string langsFolder = "lang"; string langsFolder = "lang";
if (!Directory.Exists(langsFolder)) if (!Directory.Exists(langsFolder))
Directory.CreateDirectory(langsFolder); Directory.CreateDirectory(langsFolder);
@ -26,17 +27,17 @@ namespace JetKarmaBot
string langName = Path.GetFileNameWithoutExtension(langFilePath); string langName = Path.GetFileNameWithoutExtension(langFilePath);
string langKey = langName.ToLowerInvariant(); string langKey = langName.ToLowerInvariant();
locales[langKey] = new Locale(JObject.Parse(File.ReadAllText(langFilePath)), langKey); locales[langKey] = new Locale(JObject.Parse(File.ReadAllText(langFilePath)), langKey);
Log("Found " + langName); log.Debug("Found " + langName);
} }
catch (Exception e) catch (Exception e)
{ {
Log($"Error while parsing {langFilePath}!"); log.Error($"Error while parsing {langFilePath}!");
Log(e); log.Error(e);
} }
} }
if (locales.Any()) if (locales.Any())
Log("Initialized!"); log.Info("Initialized!");
else else
throw new FileNotFoundException($"No locales found in {langsFolder}!"); throw new FileNotFoundException($"No locales found in {langsFolder}!");
} }
@ -49,15 +50,21 @@ namespace JetKarmaBot
return locales[locale]; return locales[locale];
} }
} }
private static Logger log = LogManager.GetCurrentClassLogger();
public Locale FindByCommonName(string name) public Locale FindByCommonName(string name)
{ {
log.ConditionalTrace("Trying to find locale " + name);
foreach (Locale l in locales.Values) foreach (Locale l in locales.Values)
{ {
if (l.CommonNames.Contains(name)) if (l.CommonNames.Contains(name))
{ {
log.ConditionalTrace("Found locale " + l.Name);
return l; return l;
} }
} }
log.Warn("Failed to find locale " + name);
return null; return null;
} }
public bool ContainsLocale(string locale) public bool ContainsLocale(string locale)
@ -66,9 +73,6 @@ namespace JetKarmaBot
return locales.ContainsKey(locale); return locales.ContainsKey(locale);
} }
void Log(string Message)
=> Console.WriteLine($"[{nameof(Localization)}]: {Message}");
void Log(Exception e) => Console.WriteLine(e); void Log(Exception e) => Console.WriteLine(e);
public class Locale public class Locale