Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Created May 24, 2021 15:48
Show Gist options
  • Select an option

  • Save Axemasta/0aae7072eba541b8336177b1c0f4c9cd to your computer and use it in GitHub Desktop.

Select an option

Save Axemasta/0aae7072eba541b8336177b1c0f4c9cd to your computer and use it in GitHub Desktop.
Global Logging
namespace Global.Logging
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class App : PrismApplication
{
protected override async void OnInitialized()
{
InitializeComponent();
SetForDebugging();
Log.Listeners.Add(new DelegateLogListener(OnLogListener));
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
}
/// <summary>
/// Called By Xamarin Forms Internal Log Listener
/// </summary>
/// <param name="arg1"></param>
/// <param name="arg2"></param>
private void OnLogListener(string arg1, string arg2)
{
Debug.WriteLine($"App - OnLogListener - {arg1}: {arg2}");
}
/// <summary>
/// Called When Unhandled Exception Occurs In A Task
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
foreach (var exception in e.Exception.InnerExceptions)
{
Debug.WriteLine($"App - OnUnobservedTaskException - An exception occured: {exception.ToString()}");
Crashes.TrackError(exception);
}
}
/// <summary>
/// Called When Unhandle Exception Occurs In App Domain
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
if (exception == null)
{
Debug.WriteLine("App - OnUnhandledException - Could not cast UnhandledExceptionEventArgs ExceptionObject to exception...");
return;
}
Debug.WriteLine($"App - OnUnhandledException - An exception occured: {exception}");
Crashes.TrackError((Exception)e.ExceptionObject);
}
private void SetForDebugging()
{
Xamarin.Forms.Internals.Log.Listeners.Add(new DebugListener());
}
private class DebugListener : Xamarin.Forms.Internals.LogListener
{
public override void Warning(string category, string message) =>
Debug.WriteLine($" {category}: {message}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment