using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Tango.Logging;
namespace Tango.Logging
{
/// <summary>
/// Represents an optimized WPF global exception trapper.
/// </summary>
/// <seealso cref="Tango.Logging.IGlobalExceptionTrapper" />
public class WpfGlobalExceptionTrapper : IGlobalExceptionTrapper
{
private DateTime _lastGlobalExceptionTime = DateTime.Now.AddMinutes(-1);
/// <summary>
/// Occurs when the global exception trapper has detected an unhandled exception.
/// </summary>
public event EventHandler<ApplicationCrashedEventArgs> ApplicationCrashed;
/// <summary>
/// Initializes the specified application.
/// </summary>
/// <param name="app">The application.</param>
public void Initialize(Application app)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
app.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
/// <summary>
/// Handles the UnobservedTaskException event of the TaskScheduler control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="UnobservedTaskExceptionEventArgs"/> instance containing the event data.</param>
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
e.SetObserved();
OnApplicationCrash(e.Exception);
}
/// <summary>
/// Handles the DispatcherUnhandledException event of the Current control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = OnApplicationCrash(e.Exception);
}
/// <summary>
/// Handles the UnhandledException event of the Dispatcher control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = OnApplicationCrash(e.Exception);
}
/// <summary>
/// Handles the UnhandledException event of the CurrentDomain control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
OnApplicationCrash(new Exception(e.ExceptionObject.ToStringSafe()));
}
/// <summary>
/// Called when any unhandled application exception has occurred and write to log.
/// </summary>
/// <param name="error">The error.</param>
private bool OnApplicationCrash(Exception exception)
{
if (DateTime.Now < _lastGlobalExceptionTime.AddSeconds(1))
{
LogManager.Default.Log(exception, LogCategory.Critical);
_lastGlobalExceptionTime = DateTime.Now;
return true;
}
_lastGlobalExceptionTime = DateTime.Now;
LogManager.Default.Log("Application Crashed", LogCategory.Critical);
LogManager.Default.Log(exception, LogCategory.Critical);
ApplicationCrashedEventArgs e = new ApplicationCrashedEventArgs(exception);
ApplicationCrashed?.Invoke(this, e);
if (e.TryRecover)
{
LogManager.Default.Log("Trying application recovery. Ignoring exception...");
}
return e.TryRecover;
}
}
}