From a0b202d418ca487e726a9e3f2896d055d395dd1e Mon Sep 17 00:00:00 2001 From: liuwf Date: Tue, 20 May 2025 14:54:47 +0800 Subject: [PATCH] =?UTF-8?q?change-=E4=BF=AE=E5=A4=8Dwpf=E9=97=AA=E9=80=80?= =?UTF-8?q?=E6=8D=95=E6=8D=89=E5=8F=8A=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.WPF/App.xaml.cs | 111 +++++++++++++++++++++++++++++++++++++- SlnMesnac.WPF/Startup.cs | 3 ++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index b94d00a..3486eb5 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -5,8 +5,11 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using SlnMesnac.Config; +using SlnMesnac.Serilog; using System; +using System.Diagnostics; using System.IO; +using System.Threading.Tasks; using System.Windows; namespace SlnMesnac.WPF @@ -16,6 +19,8 @@ namespace SlnMesnac.WPF /// public partial class App : Application { + private static SerilogHelper? serilogHelper; + private System.Threading.Mutex? mutex = null; private LierdaCracker cracker = new LierdaCracker(); public static IServiceProvider? ServiceProvider = null; @@ -23,6 +28,14 @@ namespace SlnMesnac.WPF // Startup事件 protected override async void OnStartup(StartupEventArgs e) { + //UI线程未捕获异常处理事件(UI主线程) + this.DispatcherUnhandledException += App_DispatcherUnhandledException; + + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + //Task线程内未捕获异常处理事件 + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常 + bool ret; mutex = new System.Threading.Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out ret); if (!ret) @@ -40,9 +53,12 @@ namespace SlnMesnac.WPF await host.StartAsync(); + serilogHelper = host.Services.GetRequiredService(); + if (serilogHelper != null) + { + serilogHelper.Info("系统初始化完成"); + } var appConfig = host.Services.GetService(); - var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; - Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}"); } /// @@ -64,11 +80,102 @@ namespace SlnMesnac.WPF // Exit事件 protected override void OnExit(ExitEventArgs e) { + if (serilogHelper != null) + { + serilogHelper.Info("点击退出"); + } + base.OnExit(e); Log.Information($"系统退出,当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); // 释放资源 // ... } + + private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + //记录日志 + Console.WriteLine(e.Exception.Message); + } + + private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + try + { + HandleException(e.Exception); + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + e.Handled = true; + } + } + + private static void HandleException(Exception ex) + { + // 记录异常的详细信息 + Log.Warning($"全局异常捕获处理: {ex.Message}"); + + // 获取堆栈跟踪信息 + StackTrace stackTrace = new StackTrace(ex, true); + + // 遍历堆栈跟踪的每一帧 + for (int i = 0; i < stackTrace.FrameCount; i++) + { + StackFrame frame = stackTrace.GetFrame(i); + + // 获取文件名、行号和方法名 + string fileName = frame.GetFileName(); + int lineNumber = frame.GetFileLineNumber(); + string methodName = frame.GetMethod().Name; + + // 记录堆栈信息 + Log.Warning($" 在文件 {fileName} 中,第 {lineNumber} 行,方法 {methodName} 抛出异常"); + } + } + + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + try + { + var exception = e.ExceptionObject as Exception; + if (exception != null) + { + HandleException(exception); + } + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + //ignore + } + } + + private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + try + { + var exception = e.Exception as Exception; + if (exception != null) + { + HandleException(exception); + } + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + e.SetObserved(); + } + } } } \ No newline at end of file diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index b4841e9..96df723 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -26,6 +26,9 @@ namespace SlnMesnac.WPF { services.AddControllers(); + // 注册 SerilogHelper 服务 + services.AddSingleton(); + //注册AppConfig services.AddSingleton(provider => {