当前位置: 首页 > 编程语言 > C#

处理全局异常Xamarin-机器人-iOS分享

时间:2023-04-10 21:26:05 C#

处理全局异常Xamarin|机器人|它可能是内存性能分辨率架构实现等任何东西。我们永远不知道什么时候以及什么原因导致应用程序在运行应用程序时崩溃是一个大问题,它可能随时发生,例如应用程序启动、加载屏幕、API调用、绑定数据、加载图片等。相信我,有时很难找到应用程序出现问题的位置和原因。我在论坛、技术社区和群组中看到过许多与同一问题相关的帖子,人们通常会在这些帖子中提问:Appcrashesonpost。在启动画面上加载应用程序时崩溃。图像显示时应用程序崩溃。从api绑定数据时应用程序崩溃。如何识别问题及其原因?目的:我们的目的是获取异常堆栈跟踪数据,以帮助我们确定导致ReleaseMode或DebugMode问题的原因。我们将能够理解问题及其产生的原因。我们将此数据存储在将存储在设备存储中的文本文件中。解决方案:或者,您可以制作自己的洞察力采集器,如果在测试您的应用程序时出现问题,它将为您提供应用程序洞察力和线索。这将是你的,你可以随意调整它。让我们尝试在全局范围内捕获{}。{}创建一个Helper类文件,该文件具有为异常数据生成文本文件的方法。publicstaticclassExceptionFileWriter{#region属性文件路径staticstringFilePath{get{stringpath=string.Empty;var_fileName="Fatal.txt";#if__IOS__stringdocumentsPath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//文档文件夹C:ddddddstringlibraryPath=Path.Combine(documentsPath,"..","Library");//库文件夹C:dddd...librarypath=Path.Combine(libraryPath,_fileName);//c:ddddd...libraryNBCCSeva.db3#else#if__ANDROID__stringdir=Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(),"Exception");如果(Directory.Exists(dir))返回Path.Combine(dir,_fileName);path=Path.Combine(Directory.CreateDirectory(dir).FullName,_fileName);#endif#endif返回路径;}}#endregion#regionToLogExceptionpublicstaticvoidToLogUnhandledException(thisExceptionexception){try{varerrorMessage=String.Format("Time:{0}rnError:UnhandledExceptionrn{1}nn",DateTime.Now,string.IsNullOrEmpty(exception.StackTrace)?异常.ToString():异常.StackTrace);File.WriteAllText(FilePath,errorMessage);}catch(Exceptionex){//只是抑制任何错误记录异常}}#endregion}是时候实现代码了:在Application文件或应用程序的SplashActivity中订阅以下事件我在这种情况下使用Application。AppDomain.CurrentDomain.UnhandledException+=CurrentDomainOnUnhandledException;TaskScheduler.UnobservedTaskException+=TaskSchedulerOnUnobservedTaskException;[Application]publicclassExceptionHandlingApp:Application{#regionConstructorpublicExceptionHandlingApp(IntPtrjavaReference,JniHandleOwnershiptransfer):base(javaReference,transfer){}#endregion#regionOnCreatepublicoverridevoidOnCreate(){base.OnCreate();}AppDomain.CurrentDomain.UnhandledException+=CurrentDomainOnUnhandledException;TaskScheduler.UnobservedTaskException+=TaskSchedulerOnUnobservedTaskException;}#endregion#region任务计划异常privatestaticvoidTaskSchedulerOnUnobservedTaskException(对象发送者,UnobservedTaskExceptionEventArgsunobservedTaskExceptionEventArgs){varnewExc=newException(“TaskSchedulerOnUnobservedTaskException”,unobservedTaskExceptionEventArgs.Exception);newExc.ToLogUnhandledException();}#endregion#regi当前域异常privatestaticvoidCurrentDomainOnUnhandledException(objectsender,UnhandledExceptionEventArgsunhandledExceptionEventArgs){varnewExc=newException("CurrentDomainOnUnhandledException",unhandledExceptionEventArgs.ExceptionObjectasException);newExc.ToLogUnhandledException();}#endregion}注意:您可以在设备存储中找到异常记录文件在文件管理器>异常文件夹>fatal.txt干杯!!结果视频全文您可以使用Xamarin.Insights而不是自己动手,因为它对Xamarin用户免费,并且已经为所有平台实现您可以在线接收使用情况报告、崩溃报告等,而无需用户手动询问您发送日志文件.当收到崩溃报告时,您唯一需要做的就是在应用程序启动时初始化Xamarin.Insights:以上是C#学习教程:HandlingglobalexceptionXamarin|机器人|iOS版分享的全部内容,如果对大家有用需要了解更多希望大家多多关注C#学习教程——Insights.HasPendingCrashReport+=(sender,isStartupCrash)=>{if(isStartupCrash){Insights.PurgePendingCrashReports().Wait();}};Insights.Initialize("您的API密钥");本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: