2015-10-18 60 views
13

我們都知道手機是一個小巧的平臺,我們必須在構建應用程序時查看很多東西。它可以是任何例如MemoryPerformanceResolutionsArchitectureImplementation等,我們不知道什麼時候和什麼原因導致應用程序崩潰的一個大問題,而與應用程序打,它可能發生任何時候處理全局異常Xamarin | Droid | iOS

例如應用程序啓動,加載屏幕,API調用,綁定數據,加載圖片等。

並且相信我有時很難找到在應用程序中導致問題的位置和原因。我在論壇,技術社區和團體中看到過許多與同一問題有關的帖子,人們通常會問的問題如下:

  1. 應用程序在啓動時崩潰。
  2. 應用程序在啓動屏幕加載時崩潰。
  3. 圖像顯示時應用程序崩潰。
  4. 應用程序在從api綁定數據時崩潰。

如何識別問題及其原因?

回答

12

目的:我們這裏的目的抓住一個異常的堆棧跟蹤數據,幫助我們找出到底是什麼導致該問題是否在Release ModeDebug 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

5

除了做你自己的,你也可以使用Xamarin.Insights,因爲它是免費使用Xamarin的用戶,並已獲得了所有平臺上的實現。 您可以在線收到使用情況報告,崩潰報告等,而無需用戶手動向您發送日誌文件。

你需要做的,收到的崩潰報告的唯一的事情就是在你的應用程序的啓動初始化Xamarin.Insights:

Insights.HasPendingCrashReport += (sender, isStartupCrash) => 
{ 
    if (isStartupCrash) { 
    Insights.PurgePendingCrashReports().Wait(); 
    } 
}; 
Insights.Initialize("Your API Key"); 
+0

當然可感謝的添加,但我想它伴隨着企業版。如果我錯了,請糾正我。 – RIYAZ

+0

至少今天它對所有Xamarin客戶來說都是免費且無限制的。當Xamarin.Insights宣佈時,有關於定價的一句話:*定價將在預覽結束時公佈,但Xamarin用戶將免費獲得一份慷慨的計劃。* https://blog.xamarin。 com/monitoring-your-apps-with-xamarin-insights/ – Wosi

+0

恩,這很好:)感謝分享@Wosi。 – RIYAZ