From 533a49d47b8463f63bf7acb83c80fe840aa13332 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 6 Jul 2025 15:08:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Helper/NlogHelper.cs | 132 ++++++++++++++++++++--------------- Helper/NotificationHelper.cs | 105 ++++++++++++++++++++++++++-- 2 files changed, 173 insertions(+), 64 deletions(-) diff --git a/Helper/NlogHelper.cs b/Helper/NlogHelper.cs index 7b096d4..66dbf14 100644 --- a/Helper/NlogHelper.cs +++ b/Helper/NlogHelper.cs @@ -3,80 +3,96 @@ using NLog; namespace PMSWPF.Helper; +/// +/// NLog 日志帮助类,提供简化的日志记录方法,并自动捕获调用者信息。 +/// public static class NlogHelper { + /// + /// 获取当前类的 NLog 日志实例。 + /// private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - - public static void Error( string msg,Exception exception=null,[CallerFilePath] string callerFilePath = "", - [CallerMemberName] string callerMember = "", - [CallerLineNumber] int callerLineNumber = 0) - { - // 使用 using 语句确保 MappedDiagnosticsLogicalContext 在作用域结束时被清理 - // 这对于异步方法尤其重要,因为上下文会随着异步操作的流转而传递 - using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) - { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) - { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) - { - Logger.Error(exception,msg); - } // 当 using 块结束时,"user-id" 和 "transaction-id" 会自动从上下文中移除 - } - } - } - public static void Info( string msg,[CallerFilePath] string callerFilePath = "", + /// + /// 记录一个错误级别的日志。 + /// + /// 日志消息内容。 + /// 可选:要记录的异常对象。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void Error(string msg, Exception exception = null, + [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0) { - // 使用 using 语句确保 MappedDiagnosticsLogicalContext 在作用域结束时被清理 - // 这对于异步方法尤其重要,因为上下文会随着异步操作的流转而传递 + // 使用 using 语句确保 MappedDiagnosticsLogicalContext 在作用域结束时被清理。 + // 这对于异步方法尤其重要,因为上下文会随着异步操作的流转而传递。 using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) - { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) - { - Logger.Info(msg); - } // 当 using 块结束时,"user-id" 和 "transaction-id" 会自动从上下文中移除 - } + Logger.Error(exception, msg); } } - - public static void Warn( string msg,[CallerFilePath] string callerFilePath = "", + + /// + /// 记录一个信息级别的日志。 + /// + /// 日志消息内容。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void Info(string msg, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0) + { + using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) + { + Logger.Info(msg); + } + } + + /// + /// 记录一个警告级别的日志。 + /// + /// 日志消息内容。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void Warn(string msg, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0) + { + using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) + { + Logger.Warn(msg); + } + } + + /// + /// 记录一个跟踪级别的日志。 + /// + /// 日志消息内容。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void Trace(string msg, + [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMember = "", [CallerLineNumber] int callerLineNumber = 0) { - // 使用 using 语句确保 MappedDiagnosticsLogicalContext 在作用域结束时被清理 - // 这对于异步方法尤其重要,因为上下文会随着异步操作的流转而传递 using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) - { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) - { - Logger.Warn(msg); - } // 当 using 块结束时,"user-id" 和 "transaction-id" 会自动从上下文中移除 - } - } - } - - public static void Trace( string msg,[CallerFilePath] string callerFilePath = "", - [CallerMemberName] string callerMember = "", - [CallerLineNumber] int callerLineNumber = 0) - { - // 使用 using 语句确保 MappedDiagnosticsLogicalContext 在作用域结束时被清理 - // 这对于异步方法尤其重要,因为上下文会随着异步操作的流转而传递 - using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) - { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) - { - using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) - { - Logger.Trace(msg); - } // 当 using 块结束时,"user-id" 和 "transaction-id" 会自动从上下文中移除 - } + Logger.Trace(msg); } } - } \ No newline at end of file diff --git a/Helper/NotificationHelper.cs b/Helper/NotificationHelper.cs index bc3e0e5..320e219 100644 --- a/Helper/NotificationHelper.cs +++ b/Helper/NotificationHelper.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using CommunityToolkit.Mvvm.Messaging; using NLog; using PMSWPF.Enums; @@ -5,23 +6,115 @@ using PMSWPF.Message; namespace PMSWPF.Helper; +/// +/// 通知帮助类,用于显示各种类型的通知消息,并集成日志记录功能。 +/// public class NotificationHelper { + /// + /// 获取当前类的 NLog 日志实例。 + /// private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); - + + /// + /// 显示一个通用通知消息,并根据通知类型记录日志。 + /// + /// 通知消息内容。 + /// 通知类型(如信息、错误、成功等),默认为信息。 + /// 是否为全局通知(目前未使用,保留参数)。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的行号。 public static void ShowMessage(string msg, NotificationType notificationType = NotificationType.Info, - bool isGlobal = false) + bool isGlobal = false, [CallerFilePath] string callerFilePath = "", + [CallerLineNumber] int callerLineNumber = 0) { - - if (notificationType==NotificationType.Error) + // 根据通知类型记录日志 + if (notificationType == NotificationType.Error) { - Logger.Error(msg); + Logger.Error($"{msg} (File: {callerFilePath}, Line: {callerLineNumber})"); } else { - Logger.Info(msg); + Logger.Info($"{msg} (File: {callerFilePath}, Line: {callerLineNumber})"); } + + // 通过消息总线发送通知消息,以便UI层可以订阅并显示 WeakReferenceMessenger.Default.Send( new NotificationMessage(msg, notificationType)); } + + /// + /// 显示一个错误通知消息,并记录错误日志。 + /// + /// 错误消息内容。 + /// 可选:要记录的异常对象。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void ShowError(string msg, Exception exception = null, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0) + { + // 使用 using 语句确保 MappedDiagnosticsLogicalContext 在作用域结束时被清理。 + // 这对于异步方法尤其重要,因为上下文会随着异步操作的流转而传递。 + using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) + { + // 记录错误日志,包含异常信息(如果提供) + Logger.Error(exception, msg); + // 通过消息总线发送错误通知 + WeakReferenceMessenger.Default.Send( + new NotificationMessage(msg, NotificationType.Error)); + } + } + + /// + /// 显示一个成功通知消息,并记录信息日志。 + /// + /// 成功消息内容。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void ShowSuccess(string msg, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0) + { + using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) + { + // 记录信息日志 + Logger.Info(msg); + // 通过消息总线发送成功通知 + WeakReferenceMessenger.Default.Send( + new NotificationMessage(msg, NotificationType.Success)); + } + } + + /// + /// 显示一个信息通知消息,并记录信息日志。 + /// + /// 信息消息内容。 + /// 自动捕获:调用此方法的源文件完整路径。 + /// 自动捕获:调用此方法的成员或属性名称。 + /// 自动捕获:调用此方法的行号。 + public static void ShowInfo(string msg, + [CallerFilePath] string callerFilePath = "", + [CallerMemberName] string callerMember = "", + [CallerLineNumber] int callerLineNumber = 0) + { + using (MappedDiagnosticsLogicalContext.SetScoped("CallerFilePath", callerFilePath)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerLineNumber", callerLineNumber)) + using (MappedDiagnosticsLogicalContext.SetScoped("CallerMember", callerMember)) + { + // 记录信息日志 + Logger.Info(msg); + // 通过消息总线发送信息通知 + WeakReferenceMessenger.Default.Send( + new NotificationMessage(msg, NotificationType.Info)); + } + } } \ No newline at end of file