forked from amkovkov/GranuSightSoftware2
feat: Avalonia logs sink to Microsoft.Logging
This commit is contained in:
@@ -15,7 +15,8 @@
|
|||||||
"LibCamera-IPAProxy": "Information",
|
"LibCamera-IPAProxy": "Information",
|
||||||
"LibCamera-V4L2": "Information",
|
"LibCamera-V4L2": "Information",
|
||||||
"LibCamera-RPISTREAM": "Information",
|
"LibCamera-RPISTREAM": "Information",
|
||||||
"LibCamera-WRAPPER": "Information"
|
"LibCamera-WRAPPER": "Information",
|
||||||
|
"Avalonia.*": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
@@ -32,7 +33,7 @@
|
|||||||
"Uv365RelayPin": 20,
|
"Uv365RelayPin": 20,
|
||||||
"FanPin": 21,
|
"FanPin": 21,
|
||||||
"PwmFrequency": 200,
|
"PwmFrequency": 200,
|
||||||
"MaxWhitePwmDuty": 0.05,
|
"MaxWhitePwmDuty": 1.0,
|
||||||
"MaxUv254PwmDuty": 1.0,
|
"MaxUv254PwmDuty": 1.0,
|
||||||
"MaxUv365PwmDuty": 1.0
|
"MaxUv365PwmDuty": 1.0
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
using Avalonia.Logging;
|
||||||
|
|
||||||
|
public sealed class AvaloniaLogSink : ILogSink
|
||||||
|
{
|
||||||
|
private readonly ILoggerFactory _loggerFactory;
|
||||||
|
private readonly Dictionary<string, ILogger> _areaLoggers = new();
|
||||||
|
|
||||||
|
public AvaloniaLogSink(ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
_loggerFactory = loggerFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsEnabled(LogEventLevel level, string area) => true;
|
||||||
|
|
||||||
|
public void Log(LogEventLevel level, string area, object? source, string messageTemplate, params object?[] propertyValues)
|
||||||
|
{
|
||||||
|
if (!_areaLoggers.ContainsKey(area))
|
||||||
|
_areaLoggers.Add(area, _loggerFactory.CreateLogger($"Avalonia.{area}"));
|
||||||
|
|
||||||
|
var logger = _areaLoggers[area];
|
||||||
|
var logLevel = ConvertLogLevel(level);
|
||||||
|
|
||||||
|
logger.Log(logLevel, messageTemplate, propertyValues);
|
||||||
|
}
|
||||||
|
public void Log(LogEventLevel level, string area, object? source, string messageTemplate)
|
||||||
|
{
|
||||||
|
if (!_areaLoggers.ContainsKey(area))
|
||||||
|
_areaLoggers.Add(area, _loggerFactory.CreateLogger($"Avalonia.{area}"));
|
||||||
|
|
||||||
|
var logger = _areaLoggers[area];
|
||||||
|
var logLevel = ConvertLogLevel(level);
|
||||||
|
|
||||||
|
logger.Log(logLevel, messageTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LogLevel ConvertLogLevel(LogEventLevel level) =>
|
||||||
|
level switch
|
||||||
|
{
|
||||||
|
LogEventLevel.Verbose => LogLevel.Trace,
|
||||||
|
LogEventLevel.Debug => LogLevel.Debug,
|
||||||
|
LogEventLevel.Information => LogLevel.Information,
|
||||||
|
LogEventLevel.Warning => LogLevel.Warning,
|
||||||
|
LogEventLevel.Error => LogLevel.Error,
|
||||||
|
LogEventLevel.Fatal => LogLevel.Critical,
|
||||||
|
_ => LogLevel.Information
|
||||||
|
};
|
||||||
|
public static LogEventLevel ConvertLogLevel(LogLevel level) =>
|
||||||
|
level switch
|
||||||
|
{
|
||||||
|
LogLevel.Trace => LogEventLevel.Verbose,
|
||||||
|
LogLevel.Debug => LogEventLevel.Debug,
|
||||||
|
LogLevel.Information => LogEventLevel.Information,
|
||||||
|
LogLevel.Warning => LogEventLevel.Warning,
|
||||||
|
LogLevel.Error => LogEventLevel.Error,
|
||||||
|
LogLevel.Critical => LogEventLevel.Fatal,
|
||||||
|
_ => LogEventLevel.Information
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user