`
niunan
  • 浏览: 701528 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

WPF全局异常捕获

 
阅读更多

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics