【问题标题】:How can I track xamarin.mac app unhandled exception如何跟踪 xamarin.mac 应用程序未处理的异常
【发布时间】:2018-05-02 10:39:12
【问题描述】:

我想在我的 xamarin mac 应用程序中获取未处理异常的崩溃报告,例如用于跟踪 android 和 ios 应用程序异常的 hockey 应用程序。我进行了很多搜索,但没有找到任何用于跟踪未处理异常的应用程序。请推荐任何应用程序来跟踪 xamarin.mac 中的异常。提前致谢。

【问题讨论】:

    标签: xamarin xamarin.mac


    【解决方案1】:

    任务异常通常不会被观察到(即,在未等待的 Task.Run() 中发生的任何异常)。您可以通过将其放入 AppDelegate 的 DidFinishLaunching 覆盖中来捕获它们:

    TaskScheduler.UnobservedTaskException += 
    TaskScheduler_UnobservedTaskException;
    
    void TaskScheduler_UnobservedTaskException(object sender, 
    UnobservedTaskExceptionEventArgs e)
    {
        System.Console.WriteLine("**Unobserved Exception**");
        System.Console.WriteLine(e.Exception.Message);
    }
    

    【讨论】:

      【解决方案2】:

      我建议您自己实现它。 Main.cs 文件充当 Xamarin Mac 应用程序的主入口点。它包含一个静态的Main 方法,用于创建一个新的 Xamarin.Mac 应用程序实例:

      using AppKit;
      
      namespace Test
      {
          static class MainClass
          {
              static void Main(string[] args)
              {
                  NSApplication.Init();
                  NSApplication.Main(args);
              }
          }
      }
      

      您可以尝试在此处设置处理程序(NSApplication.Init(); 之后:

      System.AppDomain.CurrentDomain.UnhandledException 
          += CurrentDomain_UnhandledException;
      
      string a = null;
      System.Diagnostics.Debug.WriteLine(a.Length);
      

      最后两行当然是强制空指针异常。

      缺少一件:

      private static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
      {
          System.Diagnostics.Debug.WriteLine("oh no");
      }
      

      希望这会有所帮助。

      【讨论】:

      • 您好 Thomas,感谢您的宝贵支持。我已经尝试将代码作为您给定的示例工作得很好,但没有在整个应用程序中捕获任何未处理的异常。在我的应用中浏览了几页后,应用崩溃了,但上面的代码没有捕捉到未处理的异常。
      • 我很遗憾听到我的建议没有按预期工作。根据文档,CurrentDomain 获取当前线程的应用程序域。也许没有引发处理程序的代码是在另一个线程中执行的?不幸的是,我没有其他想法。
      • 此解决方案未捕获的崩溃可能发生在核心图形绘图层
      • 好的。谢谢托马斯
      • Stepheaw 有什么其他的办法来捕捉这种类型的异常吗?
      猜你喜欢
      • 1970-01-01
      • 2017-05-08
      • 2021-06-16
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多