我们都知道手机是一个紧凑的平台,我们在构建应用程序时必须考虑很多东西。它可以是任何东西,例如Memory
Performance
Resolutions
Architecture
Implementation
等等。我们永远不知道什么时候和什么原因导致应用程序崩溃,这是一个大问题,在玩应用程序时,它可能随时发生
。应用程序启动,加载屏幕,API调用,绑定数据,加载图像等
相信我,有时候很难找到在哪里和什么原因导致应用程序中的问题。我在论坛,技术社区和小组中看到许多与相同问题相关的帖子,人们通常会问这样的问题:
- 应用程序启动时崩溃 应用程序在启动画面加载时崩溃。
- 应用程序崩溃时显示图像。 应用程序在绑定api数据时崩溃。
如何识别问题及其原因?
目的:我们这里的目的是获取异常的堆栈跟踪数据,帮助我们确定究竟是什么导致了Release Mode
或Debug Mode
中的问题。我们将能够理解这个问题和导致它的原因。我们将这些数据存储在text
文件中,该文件将存储在设备存储器中。
解决方案:或者你可以制作自己的insight grabber,这将为你的应用提供洞察力和线索,如果在测试应用时出现问题。这将是你的,你可以按自己的意愿进行调整。让我们深入到try{}
和catch{}
全局。
创建一个Helper Class
文件,该文件具有为异常数据生成文本文件的方法。
public static class ExceptionFileWriter
{
#region Property File Path
static string FilePath
{
get
{
string path = string.Empty;
var _fileName = "Fatal.txt";
#if __IOS__
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Documents folder C:ddddd
string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder C:dddd...library
path = Path.Combine(libraryPath, _fileName); //c:ddddd...libraryNBCCSeva.db3
#else
#if __ANDROID__
string dir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "Exception");
if (Directory.Exists(dir))
return Path.Combine(dir, _fileName);
path= Path.Combine(Directory.CreateDirectory(dir).FullName, _fileName);
#endif
#endif
return path;
}
}
#endregion
#region ToLog Exception
public static void ToLogUnhandledException(this Exception exception)
{
try
{
var errorMessage = String.Format("Time: {0}rnError: Unhandled Exceptionrn{1}nn", DateTime.Now, string.IsNullOrEmpty(exception.StackTrace) ? exception.ToString() : exception.StackTrace);
File.WriteAllText(FilePath, errorMessage);
}
catch (Exception ex)
{
// just suppress any error logging exceptions
}
}
#endregion
}
是时候实现代码了:在应用程序的Application
或Splash Activity
文件中订阅以下事件。本例中我使用的是Application
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
[Application]
public class ExceptionHandlingApp : Application
{
#region Constructor
public ExceptionHandlingApp(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
#endregion
#region OnCreate
public override void OnCreate()
{
base.OnCreate();
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
}
#endregion
#region Task Schedular Exception
private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
{
var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
newExc.ToLogUnhandledException();
}
#endregion
#region Current Domain Exception
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
newExc.ToLogUnhandledException();
}
#endregion
}
注意:您可以在设备存储|文件管理器>异常文件夹>fatal.txt
干杯! !
除了自己做之外,您还可以使用Xamarin。洞察对Xamarin用户来说是免费的,并且已经在所有平台上实现了。您可以在线接收使用报告,崩溃报告等,而不需要用户手动发送日志文件。
接收崩溃报告所需要做的唯一事情就是初始化Xamarin。关于应用程序启动的见解:
Insights.HasPendingCrashReport += (sender, isStartupCrash) =>
{
if (isStartupCrash) {
Insights.PurgePendingCrashReports().Wait();
}
};
Insights.Initialize("Your API Key");