Log: Use Serilog directly

This commit is contained in:
Liza 2023-02-17 18:36:55 +01:00
parent e624c5b628
commit 8986b368c7
2 changed files with 14 additions and 4 deletions

View File

@ -1,7 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Serilog.Events; using Serilog.Events;
@ -24,12 +23,18 @@ namespace Pal.Client.DependencyInjection.Logging
where TState : notnull where TState : notnull
=> _scopeProvider?.Push(state) ?? NullScope.Instance; => _scopeProvider?.Push(state) ?? NullScope.Instance;
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None && PluginLogDelegate.IsEnabled(ToSerilogLevel(logLevel)); public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None && IsEnabled(ToSerilogLevel(logLevel));
private bool IsEnabled(LogEventLevel logEventLevel) => PluginLogDelegate.IsEnabled(logEventLevel);
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter) Func<TState, Exception?, string> formatter)
{ {
if (!IsEnabled(logLevel)) if (logLevel == LogLevel.None)
return;
LogEventLevel logEventLevel = ToSerilogLevel(logLevel);
if (!IsEnabled(logEventLevel))
return; return;
if (formatter == null) if (formatter == null)
@ -52,7 +57,7 @@ namespace Pal.Client.DependencyInjection.Logging
}, },
sb); sb);
sb.Append(_name).Append(": ").Append(formatter(state, null)); sb.Append(_name).Append(": ").Append(formatter(state, null));
PluginLogDelegate.Write(ToSerilogLevel(logLevel), exception, sb.ToString()); PluginLogDelegate.Write(logEventLevel, exception, sb.ToString());
} }
private LogEventLevel ToSerilogLevel(LogLevel logLevel) private LogEventLevel ToSerilogLevel(LogLevel logLevel)

View File

@ -206,6 +206,11 @@ namespace Pal.Client
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
} }
catch (TaskCanceledException e)
{
_logger.LogError(e, "Task cancelled");
chat?.Error("Plugin was unloaded before it finished loading.");
}
catch (Exception e) catch (Exception e)
{ {
_logger.LogError(e, "Async load failed"); _logger.LogError(e, "Async load failed");