zoukankan      html  css  js  c++  java
  • wpf 全局异常捕获处理

    namespace WpfGridChange
    {
        using System;
        using System.CodeDom.Compiler;
        using System.Diagnostics;
        using System.Windows;
        using System.Threading.Tasks;
    
        public class App : Application
        {
            [DebuggerNonUserCode, GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
            public void InitializeComponent()
            {
                 base.StartupUri = new Uri("../Resources/xaml/WinWellcome.xaml", UriKind.Relative);
               
            }
    
            protected override void OnStartup(StartupEventArgs e)
            {
                RegisterEvents();
                base.OnStartup(e);
            }
    
            /// <summary>
            /// 注册事件
            /// </summary>
            private void RegisterEvents()
            {
                //Task线程内未捕获异常处理事件
                TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
    
                //UI线程未捕获异常处理事件(UI主线程)
                this.DispatcherUnhandledException += App_DispatcherUnhandledException;
    
                //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            }
    
            private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
            {
                try
                {
                    var exception = e.Exception as Exception;
                    if (exception != null)
                    {
                        HandleException(exception);
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    e.SetObserved();
                }
            }
    
            //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)      
            private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                try
                {
                    var exception = e.ExceptionObject as Exception;
                    if (exception != null)
                    {
                        HandleException(exception);
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    //ignore
                }
            }
    
            //UI线程未捕获异常处理事件(UI主线程)
            private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
            {
                try
                {
                    HandleException(e.Exception);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    //处理完后,我们需要将Handler=true表示已此异常已处理过
                    e.Handled = true;
                }
            }
            private static void HandleException(Exception e)
            {
                MessageBox.Show("程序异常:" + e.Source + "
    @@" + Environment.NewLine + e.StackTrace + "
    ##" + Environment.NewLine + e.Message);
    
                //记录日志
                Utils.LogWrite(e);
    
            }
    
    
            [GeneratedCode("PresentationBuildTasks", "4.0.0.0"), DebuggerNonUserCode, STAThread]
            public static void Main()
    { App app
    = new App(); app.InitializeComponent(); app.Run(); } } }
    Utils.LogWrite
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.IO;
    using System.Net;
    using System.Threading;
    
    namespace WpfGridChange
    {
        public class Utils
        {
    
           
            //读写锁,当资源处于写入模式时,其他线程写入需要等待本次写入结束之后才能继续写入
            private static readonly ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
            public static void LogWrite(Exception ex)
            {
                if (!Directory.Exists("Log"))
                {
                    Directory.CreateDirectory("Log");
                }
                var now = DateTime.Now;
                var logpath = @"Log" + now.Year + "" + now.Month + "" + now.Day + ".log";
                var log = "
    ----------------------" + DateTime.Now + " --------------------------
    "
                          + ex.Message
                          + "
    "
                          + ex.InnerException
                          + "
    "
                          + ex.StackTrace
                          + "
    ----------------------footer--------------------------
    ";
                try
                {
                    //设置读写锁为写入模式独占资源,其他写入请求需要等待本次写入结束之后才能继续写入
                    LogWriteLock.EnterWriteLock();
                    File.AppendAllText(logpath, log);
                }
                finally
                {
                    //退出写入模式,释放资源占用
                    LogWriteLock.ExitWriteLock();
                }
            }
    
    
        }
    }
  • 相关阅读:
    浏览器中包含什么?三个常驻线程?
    TCP粘包和拆包
    TCP有限状态机
    TCP的拥塞控制
    TCP滑动窗口实现流量控制
    http状态码及意义
    OSI七层结构
    浏览器的event loop
    history api
    表单提交的方式
  • 原文地址:https://www.cnblogs.com/Fooo/p/13201072.html
Copyright © 2011-2022 走看看