记录 Xamarin 未处理(Android 未捕获)异常 [英] Logging Xamarin unhandled (Android uncaught) exceptions

查看:31
本文介绍了记录 Xamarin 未处理(Android 未捕获)异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录未处理的异常,但我看到关于这是否可能以及如何可能的相互矛盾的信息.

I'd like to log unhandled exceptions, but I am seeing conflicting information on if and how this may be possible.

我了解 Xamarin 会引发 AndroidEnvironment.UnhandledExceptionRaiserAppDomain.CurrentDomain.UnhandledException 事件,并且我可以订阅此事件,但我的印象是 Android正在终止我的进程,而且我无权访问 Android.Utils.Log 或文件系统.

I understand that Xamarin raises an AndroidEnvironment.UnhandledExceptionRaiser or AppDomain.CurrentDomain.UnhandledException event and that I can subscribe to this event, but I am under the impression that Android is killing my process and that I don't have access to Android.Utils.Log or the file system.

如果您查看 Xamarin 的 AdvancedAppLifecycleDemos/HandlingCrashes/App.cs 有一个令人信服的论点,即您无法记录此异常.

If you take a look at Xamarin's AdvancedAppLifecycleDemos/HandlingCrashes/App.cs there's a compelling argument that you can't log this exception.

    /// <summary>
    /// When app-wide unhandled exceptions are hit, this will handle them. Be aware however, that typically
    /// android will be destroying the process, so there's not a lot you can do on the android side of things,
    /// but your xamarin code should still be able to work. so if you have a custom err logging manager or 
    /// something, you can call that here. You _won't_ be able to call Android.Util.Log, because Dalvik
    /// will destroy the java side of the process.
    /// </summary>
    protected void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
        Exception e = (Exception)args.ExceptionObject;

        // log won't be available, because dalvik is destroying the process
        //Log.Debug (logTag, "MyHandler caught : " + e.Message);
        // instead, your err handling code shoudl be run:
        Console.WriteLine("========= MyHandler caught : " + e.Message);
    }

那么如何记录未处理的异常?

So how can an unhandled exception be logged?

推荐答案

Xamarin Insights、HockeyApp 或其他崩溃记录器在应用程序完成进程之前以某种方式保存崩溃数据.一旦应用程序重新启动,它们就会将数据发送到服务器(由于进程被终止,它们无法立即发送数据).所以这是完全可能的.虽然我不确定他们是否将崩溃数据保存到设备存储(最有可能)或本地 SQLite 数据库.

Xamarin Insights, HockeyApp or other crash loggers save crash data somehow before the app finishes the process. They send data to the server once the app gets restarted (they cannot send it right away as the process is being killed). So that´s totally possible. Though I´m not sure if they save crash data to device storage (most probably) or to a local SQLite database.

这是 HockeyApp 用于捕获未处理异常的代码.它可以给你一些想法:

This the code HockeyApp uses to catch unhandled exceptions. It could give you some ideas:

AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) =>
{
    TraceWriter.WriteTrace(args.Exception);
    args.Handled = true;
};

AppDomain.CurrentDomain.UnhandledException += (sender, args) => 
    TraceWriter.WriteTrace(args.ExceptionObject);

// Wire up the unobserved task exception handler
TaskScheduler.UnobservedTaskException +=  (sender, args) =>
    TraceWriter.WriteTrace(args.Exception);

我建议尝试使用任何这些方法将文本文件写入本地存储

I would suggest to try and write a text file to the local storage within any of those methods

这篇关于记录 Xamarin 未处理(Android 未捕获)异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆