Mục đích: mục đích của chúng tôi ở đây để lấy stack trace dữ liệu của một ngoại lệ mà giúp chúng tôi xác định chính xác nguyên nhân gây ra vấn đề cho dù trong Release Mode
hoặc Debug Mode
. Chúng tôi sẽ có thể hiểu được vấn đề và lý do gây ra nó. Chúng tôi sẽ lưu trữ dữ liệu này trong một tệp text
sẽ được lưu trữ trong bộ nhớ thiết bị.
Giải pháp: Hoặc bạn có thể làm cho grabber cái nhìn sâu sắc của riêng bạn mà sẽ cung cấp cho bạn cái nhìn sâu sắc bạn ứng dụng và đầu mối nếu một cái gì đó đã đi sai trong khi thử nghiệm các ứng dụng. Nó sẽ là của bạn, bạn có thể tinh chỉnh như bạn muốn. chúng ta hãy đi đến số try{}
và catch{}
trên toàn cầu.
Tạo tệp Helper Class
có phương thức tạo tệp văn bản cho dữ liệu ngoại lệ.
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
}
Thời gian để thực hiện mã: Theo dõi sự kiện sau đây bên Application
tập tin của ứng dụng hoặc Splash Activity
. Tôi đang sử dụng Ứng dụng trong trường hợp này.
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
}
Lưu ý: Bạn có thể tìm thấy ngoại lệ tập kỷ lục trong thiết bị lưu trữ | Trình quản lý tệp> Thư mục ngoại lệ> fatal.txt
Chúc mừng !!
Result Video
Full Article
Dĩ nhiên có thể sử dụng Cảm ơn để thêm, nhưng tôi đoán nó cùng với Business Edition. Nêu tôi sai vui long chân chỉnh tôi. – RIYAZ
Ít nhất là cho ngày hôm nay nó miễn phí và không giới hạn cho tất cả khách hàng của Xamarin. Khi Xamarin.Insights đã được thông báo có câu này về giá cả: * Giá sẽ được công bố vào cuối bản xem trước, nhưng một kế hoạch hào phóng sẽ được bao gồm cho người đăng ký Xamarin mà không mất thêm phí. * Https: //blog.xamarin. com/monitoring-your-apps-with-xamarin-insights/ – Wosi
Vâng, Thật tuyệt vời :) Cảm ơn bạn đã chia sẻ @Wosi. – RIYAZ