From c318eb7ad0215b9061f2ee364cf2c1939f4d64f3 Mon Sep 17 00:00:00 2001
From: Jheam Storch Ross <58143641+srjheam@users.noreply.github.com>
Date: Sun, 20 Dec 2020 15:38:52 -0300
Subject: [PATCH 1/2] Added first implementation of a logger
---
calculatorUICOOP.Android/Helpers/LogLevel.cs | 11 +++++
calculatorUICOOP.Android/Helpers/Logger.cs | 42 +++++++++++++++++++
calculatorUICOOP.Android/MainActivity.cs | 28 +++++++++++--
.../calculatorUICOOP.Android.csproj | 2 +
4 files changed, 79 insertions(+), 4 deletions(-)
create mode 100644 calculatorUICOOP.Android/Helpers/LogLevel.cs
create mode 100644 calculatorUICOOP.Android/Helpers/Logger.cs
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..0912899
--- /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();
+
+ // Generate Log File
+ string logFileName = $"{DateTime.UtcNow:s}.log";
+ var logFile = new FileInfo(Path.Combine(logDir.FullName, logFileName));
+
+ using var _ = logFile.Create();
+ _logFile = logFile;
+ }
+
+ public void Log(LogLevel level, string message)
+ {
+ using var log = _logFile.AppendText();
+ 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 @@
+
+
From d0faa72aa68f608b4a60d807da192abfd4c47878 Mon Sep 17 00:00:00 2001
From: Jheam Storch Ross <58143641+srjheam@users.noreply.github.com>
Date: Sun, 20 Dec 2020 19:34:16 -0300
Subject: [PATCH 2/2] Fixed app creating empty logs
---
calculatorUICOOP.Android/Helpers/Logger.cs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/calculatorUICOOP.Android/Helpers/Logger.cs b/calculatorUICOOP.Android/Helpers/Logger.cs
index 0912899..89980dc 100644
--- a/calculatorUICOOP.Android/Helpers/Logger.cs
+++ b/calculatorUICOOP.Android/Helpers/Logger.cs
@@ -22,17 +22,17 @@ public Logger()
if (!logDir.Exists)
logDir.Create();
- // Generate Log File
+ // Set Log File
string logFileName = $"{DateTime.UtcNow:s}.log";
var logFile = new FileInfo(Path.Combine(logDir.FullName, logFileName));
- using var _ = logFile.Create();
_logFile = logFile;
}
public void Log(LogLevel level, string message)
{
- using var log = _logFile.AppendText();
+ 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
}