From 8b86f079e5436fb71c94fe0578f0d42594dea9a6 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 5 Sep 2025 16:18:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97=E4=B8=8D?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/App.xaml.cs | 3 +++ DMS.WPF/Logging/NLogLogger.cs | 14 ++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 49c2a7b..e757d93 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -24,6 +24,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NLog; +using NLog.Web; using ILogger = Microsoft.Extensions.Logging.ILogger; using LogLevel = Microsoft.Extensions.Logging.LogLevel; @@ -94,6 +95,7 @@ public partial class App : System.Windows.Application private void ConfigureServices(IServiceCollection services) { // 注册NLogLogger作为Microsoft.Extensions.Logging.ILogger的实现 + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -203,6 +205,7 @@ public partial class App : System.Windows.Application LogManager.Setup().LoadConfigurationFromFile("Configurations/nlog.config"); loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(LogLevel.Trace); + // loggingBuilder.addn; // 添加NLog作为日志提供者 // 捕获未处理的异常并记录 AppDomain.CurrentDomain.UnhandledException += (sender, args) => diff --git a/DMS.WPF/Logging/NLogLogger.cs b/DMS.WPF/Logging/NLogLogger.cs index 41d6b74..fb733eb 100644 --- a/DMS.WPF/Logging/NLogLogger.cs +++ b/DMS.WPF/Logging/NLogLogger.cs @@ -15,9 +15,9 @@ namespace DMS.WPF.Logging; public class NLogLogger : ILogger { /// - /// 获取当前类的 NLog 日志实例。 + /// 获取指定名称的 NLog 日志实例。 /// - private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); + private readonly NLog.Logger _logger; /// /// 日志记录器名称 @@ -27,11 +27,13 @@ public class NLogLogger : ILogger public NLogLogger() { _name = nameof(NLogLogger); + _logger = NLog.LogManager.GetCurrentClassLogger(); } public NLogLogger(string name) { _name = name; + _logger = NLog.LogManager.GetLogger(name); } /// @@ -75,7 +77,7 @@ public class NLogLogger : ILogger // 如果不启用节流,则直接记录日志并返回。 if (!throttle) { - Logger.Log(level, exception, msg); + _logger.Log(level, exception, msg); return; } @@ -90,7 +92,7 @@ public class NLogLogger : ILogger _ => { // 1. 首次出现,立即记录一次原始日志。 - Logger.Log(level, exception, msg); + _logger.Log(level, exception, msg); // 2. 创建一个新的节流信息对象。 var newThrottledLog = new ThrottledLogInfo @@ -111,7 +113,7 @@ public class NLogLogger : ILogger if (finishedLog.Count > 1) { var summaryMsg = $"日志 '{msg}' 在过去 {ThrottleTimeSeconds} 秒内被调用 {finishedLog.Count} 次。"; - Logger.Log(level, summaryMsg); + _logger.Log(level, summaryMsg); } } }, null, ThrottleTimeSeconds * 1000, Timeout.Infinite); // 设置30秒后触发,且不重复。 @@ -134,7 +136,7 @@ public class NLogLogger : ILogger public bool IsEnabled(LogLevel logLevel) { - return Logger.IsEnabled(ToNLogLevel(logLevel)); + return _logger.IsEnabled(ToNLogLevel(logLevel)); } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)