我想记录未处理的异常,但是我看到了关于是否以及如何做到这一点的矛盾信息。
我知道Xamarin会触发
如果您查看Xamarin的AdvancedAppLifecycleDemos/HandlingCrashes/App.cs,会有一个有力的论据表明您无法记录此异常。
那么未处理的异常如何被记录?
我知道Xamarin会触发
AndroidEnvironment.UnhandledExceptionRaiser
或AppDomain.CurrentDomain.UnhandledException
事件,我可以订阅该事件,但我认为Android正在杀死我的进程,我无法访问Android.Utils.Log或文件系统。如果您查看Xamarin的AdvancedAppLifecycleDemos/HandlingCrashes/App.cs,会有一个有力的论据表明您无法记录此异常。
/// <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);
}
那么未处理的异常如何被记录?
Console.Writeline
似乎不起作用,但是Android.Util.Log.Debug
有效。 - Gerald Versluis