Skip to content

Commit

Permalink
Implemented Avalonia debug logger without trace facility
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkendk committed May 21, 2024
1 parent 95b040d commit 9d43685
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Duplicati/GUI/Duplicati.GUI.TrayIcon/AvaloniaRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Logging;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Themes.Fluent;
Expand Down Expand Up @@ -98,9 +99,9 @@ protected override void Run(string[] args)
builder = builder.LogToTrace();
#else
if (Environment.GetEnvironmentVariable("DEBUG_AVALONIA") == "1")
builder = builder.LogToTrace();
Logger.Sink = new ConsoleLogSink(LogEventLevel.Information);
else if (Environment.GetEnvironmentVariable("DEBUG_AVALONIA") == "2")
builder = builder.LogToTrace(Avalonia.Logging.LogEventLevel.Verbose);
Logger.Sink = new ConsoleLogSink(LogEventLevel.Verbose);
#endif

application = builder.Instance as AvaloniaApp;
Expand Down Expand Up @@ -402,4 +403,19 @@ public override void OnFrameworkInitializationCompleted()
base.OnFrameworkInitializationCompleted();
}
}

internal class ConsoleLogSink(LogEventLevel minLevel) : ILogSink
{
private readonly LogEventLevel _minLevel = minLevel;

public bool IsEnabled(LogEventLevel level, string area)
=> level >= _minLevel;

public void Log(LogEventLevel level, string area, object source, string messageTemplate)
=> Log(level, area, source, messageTemplate, null);

public void Log(LogEventLevel level, string area, object source, string messageTemplate, params object[] propertyValues)
=> Console.WriteLine($"Avalonia [{level}]: {source} {messageTemplate} {string.Join(" ", propertyValues)}");
}

}

0 comments on commit 9d43685

Please sign in to comment.