diff --git a/calculatorUICOOP.Android/Helpers/LogLevel.cs b/calculatorUICOOP.Android/Helpers/LogLevel.cs
new file mode 100644
index 0000000..6bab66d
--- /dev/null
+++ b/calculatorUICOOP.Android/Helpers/LogLevel.cs
@@ -0,0 +1,11 @@
+namespace calculatorUICOOP.Droid.Helpers
+{
+ public enum LogLevel
+ {
+ Verbose = 0,
+ Debug = 1,
+ Information = 2,
+ Warning = 3,
+ Error = 4
+ }
+}
\ No newline at end of file
diff --git a/calculatorUICOOP.Android/Helpers/Logger.cs b/calculatorUICOOP.Android/Helpers/Logger.cs
new file mode 100644
index 0000000..89980dc
--- /dev/null
+++ b/calculatorUICOOP.Android/Helpers/Logger.cs
@@ -0,0 +1,42 @@
+using System;
+using System.IO;
+
+using Android.App;
+
+namespace calculatorUICOOP.Droid.Helpers
+{
+ public class Logger
+ {
+ ///
+ /// Gets the log file.
+ ///
+ private readonly FileInfo _logFile;
+
+ public Logger()
+ {
+ // Generate Log Directory
+ const string LOG_DIRECTORY_NAME = "logs";
+ var PRIVATE_EXTERNAL_STORAGE = Application.Context.GetExternalFilesDir(null);
+ var logDir = new DirectoryInfo(Path.Combine(PRIVATE_EXTERNAL_STORAGE.AbsolutePath, LOG_DIRECTORY_NAME));
+
+ if (!logDir.Exists)
+ logDir.Create();
+
+ // Set Log File
+ string logFileName = $"{DateTime.UtcNow:s}.log";
+ var logFile = new FileInfo(Path.Combine(logDir.FullName, logFileName));
+
+ _logFile = logFile;
+ }
+
+ public void Log(LogLevel level, string message)
+ {
+ using var log = _logFile.Exists ? _logFile.AppendText() : _logFile.CreateText();
+
+ log.WriteLine($"[{DateTime.UtcNow:s}] ({level}) - {message}"); // [2020-12-24T21:30:00] (INFO) - Ho Ho Ho! Merry Log Message
+ }
+
+ public void Log(LogLevel level, Exception exception) =>
+ Log(level, $"Exception: {exception.Message}{Environment.NewLine}{exception.StackTrace}");
+ }
+}
\ No newline at end of file
diff --git a/calculatorUICOOP.Android/MainActivity.cs b/calculatorUICOOP.Android/MainActivity.cs
index 4b1b6e8..d60486b 100644
--- a/calculatorUICOOP.Android/MainActivity.cs
+++ b/calculatorUICOOP.Android/MainActivity.cs
@@ -1,17 +1,20 @@
-using System;
+using calculatorUICOOP.Droid.Helpers;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
using Android.App;
using Android.Content.PM;
-using Android.Runtime;
-using Android.Views;
-using Android.Widget;
using Android.OS;
+using Android.Runtime;
namespace calculatorUICOOP.Droid
{
[Activity(Label = "calculatorUICOOP", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
+ private Logger _logger;
+
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
@@ -22,6 +25,11 @@ protected override void OnCreate(Bundle savedInstanceState)
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
+
+ // Handling uncaught application exceptions
+ _logger = new Logger();
+ AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
+ TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
@@ -29,5 +37,17 @@ public override void OnRequestPermissionsResult(int requestCode, string[] permis
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
+ #region Handling uncaught application exceptions
+ private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
+ {
+ var exception = unhandledExceptionEventArgs.ExceptionObject as Exception;
+ _logger.Log(LogLevel.Error, exception);
+ }
+ private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
+ {
+ foreach (var exception in unobservedTaskExceptionEventArgs.Exception.InnerExceptions)
+ _logger.Log(LogLevel.Error, exception);
+ }
+ #endregion
}
}
\ No newline at end of file
diff --git a/calculatorUICOOP.Android/calculatorUICOOP.Android.csproj b/calculatorUICOOP.Android/calculatorUICOOP.Android.csproj
index fff4a4f..7787f61 100644
--- a/calculatorUICOOP.Android/calculatorUICOOP.Android.csproj
+++ b/calculatorUICOOP.Android/calculatorUICOOP.Android.csproj
@@ -58,6 +58,8 @@
+
+