Last active
April 20, 2024 08:25
-
-
Save graealex/0067c5c7722af3fcd5bbd7a82a69953e to your computer and use it in GitHub Desktop.
Avalonia SerilogSink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Avalonia.Logging; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Serilog; | |
public class SerilogSink : ILogSink | |
{ | |
private readonly LogEventLevel _level; | |
private readonly IList<string>? _areas; | |
public SerilogSink( | |
LogEventLevel minimumLevel, | |
IList<string>? areas = null) | |
{ | |
_level = minimumLevel; | |
_areas = areas?.Count > 0 ? areas : null; | |
} | |
public bool IsEnabled(LogEventLevel level, string area) | |
{ | |
return level >= _level && (_areas?.Contains(area) ?? true); | |
} | |
public void Log(LogEventLevel level, string area, object? source, string messageTemplate) | |
{ | |
if (IsEnabled(level, area)) | |
{ | |
Serilog.Log.Write(LogLevelToSerilogLevel(level), $"[{area} {source}] {messageTemplate}"); | |
} | |
} | |
public void Log(LogEventLevel level, string area, object? source, string messageTemplate, params object?[] propertyValues) | |
{ | |
if (IsEnabled(level, area)) | |
{ | |
Serilog.Log.Write(LogLevelToSerilogLevel(level), $"[{area} {source}] {messageTemplate}", propertyValues); | |
} | |
} | |
private static Serilog.Events.LogEventLevel LogLevelToSerilogLevel(LogEventLevel level) | |
{ | |
switch (level) | |
{ | |
case LogEventLevel.Verbose: | |
return Serilog.Events.LogEventLevel.Verbose; | |
case LogEventLevel.Debug: | |
return Serilog.Events.LogEventLevel.Debug; | |
case LogEventLevel.Information: | |
return Serilog.Events.LogEventLevel.Information; | |
case LogEventLevel.Warning: | |
return Serilog.Events.LogEventLevel.Warning; | |
case LogEventLevel.Error: | |
return Serilog.Events.LogEventLevel.Error; | |
case LogEventLevel.Fatal: | |
return Serilog.Events.LogEventLevel.Fatal; | |
default: | |
return Serilog.Events.LogEventLevel.Verbose; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Avalonia.Controls; | |
using Avalonia.Logging; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
public static class SerilogSinkExtensions | |
{ | |
public static T LogToSerilog<T>( | |
this T builder, | |
LogEventLevel level = LogEventLevel.Warning, | |
params string[] areas) | |
where T : AppBuilderBase<T>, new() | |
{ | |
Logger.Sink = new SerilogSink(level, areas); | |
return builder; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment