跟着《WPF專業編程開發指南》這書打的代碼的,自己在正式項目中測試通過,可以抓取到全局的異常,用的log4net來記錄日志
核心代碼:
寫在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);
}
}

