diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs index 0eb982d08..0bd9f9d1d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs @@ -9,9 +9,12 @@ using System.Threading.Tasks; using System.Windows; using Tango.BL; using Tango.Core; +using Tango.Core.DI; using Tango.Core.Helpers; using Tango.Logging; using Tango.PPC.Common; +using Tango.PPC.Common.EventLogging; +using Tango.PPC.Common.Notifications; using Tango.PPC.Common.WatchDog; using Tango.Settings; @@ -22,6 +25,8 @@ namespace Tango.PPC.UI /// </summary> public partial class App : Application { + private WpfGlobalExceptionTrapper exceptionTrapper; + public static String[] StartupArgs { get; private set; } private LogManager LogManager = LogManager.Default; @@ -47,6 +52,10 @@ namespace Tango.PPC.UI base.OnStartup(e); + exceptionTrapper = new WpfGlobalExceptionTrapper(); + exceptionTrapper.Initialize(this); + exceptionTrapper.ApplicationCrashed += ExceptionTrapper_ApplicationCrashed; + LogManager.Categories.Clear(); CoreSettings.DefaultDataSource = new DataSource() @@ -72,5 +81,66 @@ namespace Tango.PPC.UI LogManager.Categories.AddRange(settings.LoggingCategories); } + + #region Global Exception Trapping + + /// <summary> + /// Handles the ApplicationCrashed event of the ExceptionTrapper. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="ApplicationCrashedEventArgs"/> instance containing the event data.</param> + private void ExceptionTrapper_ApplicationCrashed(object sender, ApplicationCrashedEventArgs e) + { + e.TryRecover = true; + + try + { + LogManager.Log(e.Exception, "Application Crashed!"); + LogManager.Log("Trying to recover from application crash..."); + + try + { + if (Application.Current == null) + { + new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown }; + } + } + catch { } + + try + { + var eventLogger = TangoIOC.Default.GetInstance<IEventLogger>(); + if (eventLogger != null) + { + eventLogger.Log(e.Exception, "Application Crashed!"); + } + } + catch { } + + Application.Current.Dispatcher.Invoke(async () => + { + try + { + LogManager.Log("Trying to notify the user about the crash..."); + INotificationProvider notificationProvider = TangoIOC.Default.GetInstance<INotificationProvider>(); + + if (notificationProvider != null) + { + await notificationProvider.ShowError("An unexpected error has occurred. Use the application logs to diagnose and report the problem."); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error using the notification provider."); + } + }); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error in global exception trapper!"); + } + } + + #endregion } } |
