diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2017-11-16 13:38:56 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2017-11-16 13:38:56 +0200 |
| commit | 914f4db513477d9aff726546bac47545195a3e37 (patch) | |
| tree | d2ff190fd84b1dfaa03eec76563c431592ece7ff /Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs | |
| parent | 65d01ff549d80fbe13ff5e966df216c9f7c03653 (diff) | |
| download | Tango-914f4db513477d9aff726546bac47545195a3e37.tar.gz Tango-914f4db513477d9aff726546bac47545195a3e37.zip | |
Rename "Visual Studio" to "Visual_Studio"
Rename "External Repositories" to "External_Repositories".
Diffstat (limited to 'Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs b/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs new file mode 100644 index 000000000..a0b52b077 --- /dev/null +++ b/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs @@ -0,0 +1,106 @@ +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; + + /// <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) + { + OnApplicationCrash(e.Exception.ToStringSafe()); + } + + /// <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.ToStringSafe()); + } + + /// <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.ToStringSafe()); + } + + /// <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(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(String error) + { + if (DateTime.Now - _lastGlobalExceptionTime > TimeSpan.FromSeconds(1)) + { + _lastGlobalExceptionTime = DateTime.Now; + return true; + } + + _lastGlobalExceptionTime = DateTime.Now; + LogManager.OverrideQueue = true; + LogManager.Log("Application Crash!" + Environment.NewLine + error); + + ApplicationCrashedEventArgs e = new ApplicationCrashedEventArgs(error); + + ApplicationCrashed?.Invoke(this, e); + + if (e.TryRecover) + { + LogManager.Log("Trying application recovery. Ignoring exception..."); + } + + LogManager.OverrideQueue = false; + return e.TryRecover; + } + } +} |
