Skip to content

Commit 87dd675

Browse files
committed
Improve logging
1 parent d6379be commit 87dd675

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

KeyAsio.Gui/App.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
using KeyAsio.Shared.Realtime;
2222
using KeyAsio.Shared.Utils;
2323
using Milki.Extensions.Configuration;
24+
using Milki.Extensions.MixPlayer;
25+
using NLog.Extensions.Logging;
26+
2427
using OrtdpLogger = KeyAsio.MemoryReading.Logger;
2528

2629
namespace KeyAsio.Gui;
@@ -144,6 +147,10 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc
144147

145148
private void App_OnStartup(object sender, StartupEventArgs e)
146149
{
150+
Configuration.Instance.SetLogger(
151+
Microsoft.Extensions.Logging.LoggerFactory.Create(k => k.AddNLog("nlog.config")));
152+
NLogDevice.RegisterDefault();
153+
147154
UiDispatcher.SetUiSynchronizationContext(new DispatcherSynchronizationContext());
148155
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
149156
var settings = ConfigurationFactory.GetConfiguration<AppSettings>(MyYamlConfigurationConverter.Instance, ".");

KeyAsio.Gui/Utils/NLogDevice.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using ATL.Logging;
2+
using KeyAsio.Shared;
3+
using ILogger = KeyAsio.MemoryReading.Logging.ILogger;
4+
using LogLevel = KeyAsio.MemoryReading.Logging.LogLevel;
5+
6+
namespace KeyAsio.Gui.Utils;
7+
8+
internal class NLogDevice : ILogDevice
9+
{
10+
private static Log _logInstance = new();
11+
12+
private static readonly ILogger Logger = LogUtils.GetLogger("ATL");
13+
public static ILogDevice Instance { get; } = new NLogDevice();
14+
15+
public static void RegisterDefault()
16+
{
17+
LogDelegator.SetLog(ref _logInstance);
18+
_logInstance.Register(Instance);
19+
}
20+
21+
private NLogDevice()
22+
{
23+
_logInstance.Register(this);
24+
}
25+
26+
public void DoLog(Log.LogItem anItem)
27+
{
28+
var level = GetLevel(anItem.Level);
29+
Logger.Log(level, $"({anItem.Location}) {anItem.Message}");
30+
}
31+
32+
private static LogLevel GetLevel(int level)
33+
{
34+
if (level == Log.LV_DEBUG) return LogLevel.Debug;
35+
if (level == Log.LV_INFO) return LogLevel.Information;
36+
if (level == Log.LV_WARNING) return LogLevel.Warning;
37+
if (level == Log.LV_ERROR) return LogLevel.Error;
38+
return LogLevel.Information;
39+
}
40+
}

0 commit comments

Comments
 (0)