mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
Added logging
This commit is contained in:
parent
7820d50fe8
commit
66190a1eb0
@ -1,10 +1,8 @@
|
||||
<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>
|
||||
@ -13,9 +11,10 @@
|
||||
<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
14
JetKarmaBot/NLog.config
Executable 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>
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user