mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-12 21:38:38 +00:00
UI - Log Avalonia logs in Serilog instead of their own slow solution
This commit is contained in:
parent
f6f6f0fe6a
commit
eed6ff7a2b
@ -16,6 +16,7 @@ using Artemis.WebClient.Workshop.DryIoc;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Logging;
|
||||
using Avalonia.Styling;
|
||||
using DryIoc;
|
||||
using ReactiveUI;
|
||||
@ -32,9 +33,9 @@ public static class ArtemisBootstrapper
|
||||
{
|
||||
if (_application != null || _container != null)
|
||||
throw new ArtemisUIException("UI already bootstrapped");
|
||||
|
||||
|
||||
Utilities.PrepareFirstLaunch();
|
||||
|
||||
|
||||
application.RequestedThemeVariant = ThemeVariant.Dark;
|
||||
_application = application;
|
||||
_container = new Container(rules => rules
|
||||
@ -51,6 +52,8 @@ public static class ArtemisBootstrapper
|
||||
configureServices?.Invoke(_container);
|
||||
|
||||
_container.UseDryIocDependencyResolver();
|
||||
|
||||
Logger.Sink = _container.Resolve<SerilogAvaloniaSink>();
|
||||
return _container;
|
||||
}
|
||||
|
||||
|
||||
56
src/Artemis.UI/SerilogAvaloniaSink.cs
Normal file
56
src/Artemis.UI/SerilogAvaloniaSink.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Avalonia.Logging;
|
||||
using Serilog;
|
||||
using AvaloniaLogLevel = Avalonia.Logging.LogEventLevel;
|
||||
using SerilogLogLevel = Serilog.Events.LogEventLevel;
|
||||
|
||||
namespace Artemis.UI;
|
||||
|
||||
[SuppressMessage("ReSharper", "TemplateIsNotCompileTimeConstantProblem")]
|
||||
public class SerilogAvaloniaSink : ILogSink
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public SerilogAvaloniaSink(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsEnabled(AvaloniaLogLevel level, string area)
|
||||
{
|
||||
SerilogLogLevel logLevel = GetSerilogLogLevel(level, area);
|
||||
|
||||
// Except with binding errors, ignore anything that is information or lower
|
||||
return (area == "Binding" || logLevel > SerilogLogLevel.Information) && _logger.IsEnabled(logLevel);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Log(AvaloniaLogLevel level, string area, object? source, string messageTemplate)
|
||||
{
|
||||
SerilogLogLevel logLevel = GetSerilogLogLevel(level, area);
|
||||
|
||||
ILogger logger = source != null ? _logger.ForContext(source.GetType()) : _logger;
|
||||
logger.Write(logLevel, messageTemplate);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Log(AvaloniaLogLevel level, string area, object? source, string messageTemplate, params object?[] propertyValues)
|
||||
{
|
||||
SerilogLogLevel logLevel = GetSerilogLogLevel(level, area);
|
||||
|
||||
ILogger logger = source != null ? _logger.ForContext(source.GetType()) : _logger;
|
||||
logger.Write(logLevel, messageTemplate, propertyValues);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static SerilogLogLevel GetSerilogLogLevel(AvaloniaLogLevel level, string area)
|
||||
{
|
||||
// Avalonia considers binding errors warnings, we'll treat them Verbose as to not spam people's logs
|
||||
// And yes we should fix them instead but we can't always: https://github.com/AvaloniaUI/Avalonia/issues/5762
|
||||
if (area == "Binding")
|
||||
return SerilogLogLevel.Verbose;
|
||||
return (SerilogLogLevel) level;
|
||||
}
|
||||
}
|
||||
@ -243,6 +243,7 @@
|
||||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=activatable/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Avalonia/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Hotkey/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=luma/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=pixmap/@EntryIndexedValue">True</s:Boolean>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user