aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs
blob: 5ee714cb67ba9c171054d5ef28aab2f7187d416b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;
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;
        }
    }
}