perf: 日志记录增加 ClassName 输出,debug 模式下属性切换新增反引号显示

This commit is contained in:
uye
2025-08-30 23:17:45 +08:00
parent 24504d6f6c
commit 81bace8467
3 changed files with 27 additions and 8 deletions

View File

@@ -255,11 +255,11 @@ public static class ConfigFactory
if (result)
{
ConfigurationUpdateEvent?.Invoke(key, oldValue, newValue);
_logger.Debug("Configuration {Key} has been set to {NewValue}", key, newValue);
_logger.Debug("Configuration {Key} has been set to `{NewValue}`", key, newValue);
}
else
{
_logger.Warning("Failed to save configuration {Key} to {NewValue}", key, newValue);
_logger.Warning("Failed to save configuration {Key} to `{NewValue}`", key, newValue);
}
}
catch (Exception e)

View File

@@ -135,11 +135,11 @@ namespace MaaWpfGui.Helper
if (result)
{
ConfigurationUpdateEvent?.Invoke(key, old, value);
_logger.Debug("Configuration {Key} has been set to {Value}", key, value);
_logger.Debug("Configuration {Key} has been set to `{Value}`", key, value);
}
else
{
_logger.Warning("Failed to save configuration {Key} to {Value}", key, value);
_logger.Warning("Failed to save configuration {Key} to `{Value}`", key, value);
}
return result;
@@ -158,11 +158,11 @@ namespace MaaWpfGui.Helper
if (result)
{
ConfigurationUpdateEvent?.Invoke(key, old, value);
_logger.Debug("Global configuration {Key} has been set to {Value}", key, value);
_logger.Debug("Global configuration {Key} has been set to `{Value}`", key, value);
}
else
{
_logger.Warning("Failed to save global configuration {Key} to {Value}", key, value);
_logger.Warning("Failed to save global configuration {Key} to `{Value}`", key, value);
}
return result;

View File

@@ -45,6 +45,7 @@ using MaaWpfGui.Views.UI;
using MaaWpfGui.WineCompat;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Stylet;
using StyletIoC;
@@ -159,10 +160,11 @@ namespace MaaWpfGui.Main
// Bootstrap serilog
var loggerConfiguration = new LoggerConfiguration()
.WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss}][{Level:u3}] <{ThreadId}><{ThreadName}> {Message:lj}{NewLine}{Exception}")
.WriteTo.Debug(outputTemplate: "[{Timestamp:HH:mm:ss}][{Level:u3}][{ClassName}] <{ThreadId}> {Message:lj}{NewLine}{Exception}")
.WriteTo.File(
UiLogFilename,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}][{Level:u3}] <{ThreadId}><{ThreadName}> {Message:lj}{NewLine}{Exception}")
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}][{Level:u3}][{ClassName}] <{ThreadId}> {Message:lj}{NewLine}{Exception}")
.Enrich.With<ClassNameEnricher>()
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithThreadName();
@@ -285,6 +287,23 @@ namespace MaaWpfGui.Main
}
}
public class ClassNameEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (!logEvent.Properties.TryGetValue("SourceContext", out var sourceContextValue))
{
return;
}
var sourceContext = sourceContextValue.ToString().Trim('"');
var lastDotIndex = sourceContext.LastIndexOf('.');
var className = lastDotIndex >= 0 ? sourceContext[(lastDotIndex + 1)..] : sourceContext;
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty("ClassName", className));
}
}
protected override void OnLaunch()
{
BadModules.CheckAndWarnBadInjectedModules();