首页 > 技术文章 > WPF全局异常捕获

niunan 2019-05-09 19:07 原文

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

 

推荐阅读