目的:我們這裏的目的抓住一個異常的堆棧跟蹤數據,幫助我們找出到底是什麼導致該問題是否在Release Mode
或Debug Mode
。我們將能夠理解問題以及導致問題的原因。我們會將這些數據存儲在將存儲在設備存儲中的text
文件中。
解決方案:或者你可以使自己的見解採集會給你你的應用程序的洞察力和線索,如果在測試應用程序出錯了。它會是你的,你可以調整你想要的。讓我們全球潛入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\...\library\NBCCSeva.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}\r\nError: Unhandled Exception\r\n{1}\n\n", 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
以下事件。我在這種情況下使用應用程序。
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
乾杯!
Result Video
Full Article
當然可感謝的添加,但我想它伴隨着企業版。如果我錯了,請糾正我。 – RIYAZ
至少今天它對所有Xamarin客戶來說都是免費且無限制的。當Xamarin.Insights宣佈時,有關於定價的一句話:*定價將在預覽結束時公佈,但Xamarin用戶將免費獲得一份慷慨的計劃。* https://blog.xamarin。 com/monitoring-your-apps-with-xamarin-insights/ – Wosi
恩,這很好:)感謝分享@Wosi。 – RIYAZ