跟着《WPF專業編程開發指南》這書打的代碼的,本身在正式項目中測試經過,能夠抓取到全局的異常,用的log4net來記錄日誌編程
核心代碼:ide
寫在App.xaml.cs中測試
/// <summary> /// App.xaml 的交互邏輯 /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException); } void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { Common.Log.Error($"抓到未知異常:",e.Exception); } }