美文网首页
WPF捕获全局未处理异常

WPF捕获全局未处理异常

作者: KwokKwok | 来源:发表于2018-01-22 16:08 被阅读41次

WPF中捕获全局异常并记录

应用有时候会异常崩溃,这时候如果有错误的堆栈信息,就很方便我们查找问题。捕获未处理异常我们只需要在App.xaml.cs中写异常捕获逻辑即可。

public partial class App : Application
{
    public App()
    {
        Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
    }

    private void CurrentDomain_ProcessExit(object sender, EventArgs e)
    {
        Console.WriteLine("应用退出");
    }

    void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        MessageBox.Show("我们很抱歉,当前应用程序遇到一些问题,该操作已经终止,请进行重试,如果问题继续存在,请联系管理员:" + e.Exception.Message, "意外的操作", MessageBoxButton.OK, MessageBoxImage.Information);//这里通常需要给用户一些较为友好的提示,并且后续可能的操作
        e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
        ApplicationHelper.Log(e.Exception);
    }

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show("我们很抱歉,当前应用程序遇到一些问题,该操作已经终止,请进行重试,如果问题继续存在,请联系管理员:" + e.ToString(), "意外的操作", MessageBoxButton.OK, MessageBoxImage.Information);
        ApplicationHelper.Log(e);
    }
}

贴一个我平时使用的代码:

static class ApplicationHelper
{
    /// <summary>
    /// 错误记录路径
    /// </summary>
    private static string LogPath = Path.Combine(ApplicationHelper.GetAppDefaultPath(), "Log.txt");

    /// <summary>
    /// 应用默认数据路径
    /// </summary>
    /// <returns></returns>
    public static string GetAppDefaultPath()
    {
        var path = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Data", "Default");
        DirectoryInfo directory = new DirectoryInfo(path);
        if (directory.Exists)
        {
            return path;
        }
        else
        {
            directory.Create();
        }
        return path;
    }

    /// <summary>
    /// 应用产生的数据
    /// </summary>
    /// <returns></returns>
    public static string GetAppDataPath()
    {
        var path = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Data");
        DirectoryInfo directory = new DirectoryInfo(path);
        if (directory.Exists)
        {
            return path;
        }
        else
        {
            directory.Create();
        }
        return path;
    }

    /// <summary>
    /// 记录未捕获异常信息
    /// </summary>
    /// <param name="e"></param>
    public static void Log(UnhandledExceptionEventArgs e)
    {
        Log(e.ToString());
    }


    /// <summary>
    /// 记录异常
    /// </summary>
    /// <param name="e"></param>
    public static void Log(Exception e)
    {
        Log(e.ToString());
    }

    /// <summary>
    /// 记录模板。也可用于直接调用,记录一些信息
    /// </summary>
    /// <param name="error"></param>
    public static void Log(string error)
    {
        var sb = new StringBuilder();
        sb.AppendLine("*****************************************");
        sb.AppendLine(string.Format("{0}  {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
        sb.AppendLine("");
        sb.AppendLine(error);
        sb.AppendLine("*****************************************");
        sb.AppendLine("");
        sb.AppendLine("");
        File.AppendAllText(LogPath, sb.ToString());
    }
}

相关文章

  • WPF捕获全局未处理异常

    WPF中捕获全局异常并记录 应用有时候会异常崩溃,这时候如果有错误的堆栈信息,就很方便我们查找问题。捕获未处理异常...

  • WPF全局异常捕获

    跟着《WPF专业编程开发指南》这书打的代码的,自己在正式项目中测试通过,可以抓取到全局的异常,用的log4net来...

  • python3.7异常小记

    首先上示例代码: 这段代码包含了,异常分支类型捕获,无异常捕获,有无异常都会捕获的执行逻辑。 捕获全局异常 对入口...

  • 全局异常捕获

    1. 创建MyApplication extends MultiDexApplication 在oncreate初...

  • 全局异常捕获

    /** * 1.全局捕获异常类 * 2.@authorDell * 3.@date2017/9/19 17:03 ...

  • 全局异常捕获

    引言 现代前端应用面临的浏览器环境是非常复杂的,尤其是移动端页面。 面对如此多样的浏览器环境,我们需要一种异常监控...

  • 如何优雅地查看 JS 错误堆栈?

    在前端,我们经常会通过 window.onerror 事件来捕获未处理的异常。假设捕获了一个异常,上报的堆栈是这个...

  • 全局异常处理优先级

    如果controller层被注解@RestControllerAdvice,进行了异常捕获,那么全局异常捕获将不生效。

  • Spring Boot 常用技术点

    全局捕获异常 现在不管是什么项目,为了项目质量和便于维护,都会加入异常捕获,而Spring Boot 捕获异常更为...

  • Android 如何捕获崩溃异常并重启应用

    第一种方法:通过AppUncaughtExceptionHandler来捕获异常 Android全局捕获崩溃异常记...

网友评论

      本文标题:WPF捕获全局未处理异常

      本文链接:https://www.haomeiwen.com/subject/fealaxtx.html