2025-07-06 15:08:14 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
2025-07-02 22:07:16 +08:00
|
|
|
|
using NLog;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using PMSWPF.Enums;
|
|
|
|
|
|
using PMSWPF.Message;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Helper;
|
|
|
|
|
|
|
2025-07-06 15:08:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通知帮助类,用于显示各种类型的通知消息,并集成日志记录功能。
|
|
|
|
|
|
/// </summary>
|
2025-06-23 13:42:02 +08:00
|
|
|
|
public class NotificationHelper
|
|
|
|
|
|
{
|
2025-07-06 15:08:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前类的 NLog 日志实例。
|
|
|
|
|
|
/// </summary>
|
2025-07-02 22:07:16 +08:00
|
|
|
|
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
|
2025-07-06 15:08:14 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 显示一个通用通知消息,并根据通知类型记录日志。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="msg">通知消息内容。</param>
|
|
|
|
|
|
/// <param name="notificationType">通知类型(如信息、错误、成功等),默认为信息。</param>
|
|
|
|
|
|
/// <param name="isGlobal">是否为全局通知(目前未使用,保留参数)。</param>
|
|
|
|
|
|
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
|
|
|
|
|
|
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public static void ShowMessage(string msg, NotificationType notificationType = NotificationType.Info,
|
2025-07-06 15:08:14 +08:00
|
|
|
|
bool isGlobal = false, [CallerFilePath] string callerFilePath = "",
|
|
|
|
|
|
[CallerLineNumber] int callerLineNumber = 0)
|
2025-06-23 13:42:02 +08:00
|
|
|
|
{
|
2025-07-06 15:08:14 +08:00
|
|
|
|
// 根据通知类型记录日志
|
|
|
|
|
|
if (notificationType == NotificationType.Error)
|
2025-07-02 22:07:16 +08:00
|
|
|
|
{
|
2025-07-06 15:08:14 +08:00
|
|
|
|
Logger.Error($"{msg} (File: {callerFilePath}, Line: {callerLineNumber})");
|
2025-07-02 22:07:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-06 15:08:14 +08:00
|
|
|
|
Logger.Info($"{msg} (File: {callerFilePath}, Line: {callerLineNumber})");
|
2025-07-02 22:07:16 +08:00
|
|
|
|
}
|
2025-07-06 15:08:14 +08:00
|
|
|
|
|
|
|
|
|
|
// 通过消息总线发送通知消息,以便UI层可以订阅并显示
|
2025-06-23 13:42:02 +08:00
|
|
|
|
WeakReferenceMessenger.Default.Send<NotificationMessage>(
|
|
|
|
|
|
new NotificationMessage(msg, notificationType));
|
|
|
|
|
|
}
|
2025-07-06 15:08:14 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 显示一个错误通知消息,并记录错误日志。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="msg">错误消息内容。</param>
|
|
|
|
|
|
/// <param name="exception">可选:要记录的异常对象。</param>
|
|
|
|
|
|
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
|
|
|
|
|
|
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
|
|
|
|
|
|
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
|
|
|
|
|
|
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<NotificationMessage>(
|
|
|
|
|
|
new NotificationMessage(msg, NotificationType.Error));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 显示一个成功通知消息,并记录信息日志。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="msg">成功消息内容。</param>
|
|
|
|
|
|
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
|
|
|
|
|
|
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
|
|
|
|
|
|
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
|
|
|
|
|
|
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<NotificationMessage>(
|
|
|
|
|
|
new NotificationMessage(msg, NotificationType.Success));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 显示一个信息通知消息,并记录信息日志。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="msg">信息消息内容。</param>
|
|
|
|
|
|
/// <param name="callerFilePath">自动捕获:调用此方法的源文件完整路径。</param>
|
|
|
|
|
|
/// <param name="callerMember">自动捕获:调用此方法的成员或属性名称。</param>
|
|
|
|
|
|
/// <param name="callerLineNumber">自动捕获:调用此方法的行号。</param>
|
|
|
|
|
|
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<NotificationMessage>(
|
|
|
|
|
|
new NotificationMessage(msg, NotificationType.Info));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-23 13:42:02 +08:00
|
|
|
|
}
|