跟着《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); } }