Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions calculatorUICOOP.Android/Helpers/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace calculatorUICOOP.Droid.Helpers
{
public enum LogLevel
{
Verbose = 0,
Debug = 1,
Information = 2,
Warning = 3,
Error = 4
}
}
42 changes: 42 additions & 0 deletions calculatorUICOOP.Android/Helpers/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;

using Android.App;

namespace calculatorUICOOP.Droid.Helpers
{
public class Logger
{
/// <value>
/// Gets the log file.
/// </value>
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}");
}
}
28 changes: 24 additions & 4 deletions calculatorUICOOP.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,12 +25,29 @@ 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)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

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
}
}
2 changes: 2 additions & 0 deletions calculatorUICOOP.Android/calculatorUICOOP.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<PackageReference Include="Xamarin.Essentials" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\Logger.cs" />
<Compile Include="Helpers\LogLevel.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down