Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
Milkitic committed Feb 21, 2024
1 parent d6379be commit 87dd675
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions KeyAsio.Gui/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@
using KeyAsio.Shared.Realtime;
using KeyAsio.Shared.Utils;
using Milki.Extensions.Configuration;
using Milki.Extensions.MixPlayer;
using NLog.Extensions.Logging;

using OrtdpLogger = KeyAsio.MemoryReading.Logger;

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

private void App_OnStartup(object sender, StartupEventArgs e)
{
Configuration.Instance.SetLogger(
Microsoft.Extensions.Logging.LoggerFactory.Create(k => k.AddNLog("nlog.config")));
NLogDevice.RegisterDefault();

UiDispatcher.SetUiSynchronizationContext(new DispatcherSynchronizationContext());
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
var settings = ConfigurationFactory.GetConfiguration<AppSettings>(MyYamlConfigurationConverter.Instance, ".");
40 changes: 40 additions & 0 deletions KeyAsio.Gui/Utils/NLogDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using ATL.Logging;
using KeyAsio.Shared;
using ILogger = KeyAsio.MemoryReading.Logging.ILogger;
using LogLevel = KeyAsio.MemoryReading.Logging.LogLevel;

namespace KeyAsio.Gui.Utils;

internal class NLogDevice : ILogDevice
{
private static Log _logInstance = new();

private static readonly ILogger Logger = LogUtils.GetLogger("ATL");
public static ILogDevice Instance { get; } = new NLogDevice();

public static void RegisterDefault()
{
LogDelegator.SetLog(ref _logInstance);
_logInstance.Register(Instance);
}

private NLogDevice()
{
_logInstance.Register(this);
}

public void DoLog(Log.LogItem anItem)
{
var level = GetLevel(anItem.Level);
Logger.Log(level, $"({anItem.Location}) {anItem.Message}");
}

private static LogLevel GetLevel(int level)
{
if (level == Log.LV_DEBUG) return LogLevel.Debug;
if (level == Log.LV_INFO) return LogLevel.Information;
if (level == Log.LV_WARNING) return LogLevel.Warning;
if (level == Log.LV_ERROR) return LogLevel.Error;
return LogLevel.Information;
}
}

0 comments on commit 87dd675

Please sign in to comment.