From 8231c057a4073e7397dbb1d953c43a76d8187e72 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 12 Feb 2018 12:53:46 +0200 Subject: Implemented global exception trapping on machine studio. --- .../Tango.MachineStudio.UI/App.xaml.cs | 57 +++++++++++++++++++ .../Tango.MachineStudio.UI/Images/exception.png | Bin 0 -> 1528 bytes .../Tango.MachineStudio.UI.csproj | 11 ++++ .../Tango.MachineStudio.UI/ViewModelLocator.cs | 17 ------ .../Windows/ExceptionResolutions.cs | 15 +++++ .../Windows/ExceptionWindow.xaml | 61 +++++++++++++++++++++ .../Windows/ExceptionWindow.xaml.cs | 48 ++++++++++++++++ 7 files changed, 192 insertions(+), 17 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Images/exception.png create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionResolutions.cs create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml.cs (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI') diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs index 50e57aab1..ffc8068a5 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/App.xaml.cs @@ -2,10 +2,14 @@ using System.Collections.Generic; using System.Configuration; using System.Data; +using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using System.Windows; using Tango.Integration.Observables; +using Tango.Logging; +using Tango.MachineStudio.UI.Windows; +using Tango.Settings; namespace Tango.MachineStudio.UI { @@ -14,9 +18,62 @@ namespace Tango.MachineStudio.UI /// public partial class App : Application { + private WpfGlobalExceptionTrapper exceptionTrapper; + protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + + LogManager.Categories.Clear(); + LogManager.Categories.AddRange(SettingsManager.Default.MachineStudio.LoggingCategories); + + if (LogManager.Categories.Count == 0) + { + LogManager.Categories.Add(LogCategory.Critical); + LogManager.Categories.Add(LogCategory.Error); + LogManager.Categories.Add(LogCategory.General); + LogManager.Categories.Add(LogCategory.Warning); + } + + LogManager.RegisterLogger(new VSOutputLogger()); + LogManager.RegisterLogger(new FileLogger()); + + exceptionTrapper = new WpfGlobalExceptionTrapper(); + exceptionTrapper.Initialize(this); + exceptionTrapper.ApplicationCrashed += ExceptionTrapper_ApplicationCrashed; } + + #region Global Exception Trapping + + /// + /// Handles the ApplicationCrashed event of the ExceptionTrapper. + /// + /// The source of the event. + /// The instance containing the event data. + private void ExceptionTrapper_ApplicationCrashed(object sender, ApplicationCrashedEventArgs e) + { + ExceptionWindow exWin = new ExceptionWindow(e.Exception); + exWin.ShowDialog(); + + switch (exWin.Resolution) + { + case ExceptionResolutions.Ignore: + e.TryRecover = true; + break; + case ExceptionResolutions.Restart: + e.TryRecover = false; + LogManager.Log("User selection was to restart the application. Restarting..."); + Process.Start(Application.ResourceAssembly.Location); + Environment.Exit(0); + break; + case ExceptionResolutions.Shutdown: + e.TryRecover = false; + LogManager.Log("User selection was to shutdown the application. Restarting..."); + Environment.Exit(0); + break; + } + } + + #endregion } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Images/exception.png b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Images/exception.png new file mode 100644 index 000000000..9a4abb703 Binary files /dev/null and b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Images/exception.png differ diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj index 5a1479c43..4569d1439 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Tango.MachineStudio.UI.csproj @@ -160,6 +160,10 @@ ShutdownView.xaml + + + ExceptionWindow.xaml + MSBuild:Compile Designer @@ -215,6 +219,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + @@ -329,6 +337,9 @@ + + + $(TargetDir)linkgen.exe -s "$(TargetPath)" -d "$(TargetDir)Utilities\Machine Studio.lnk" diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs index daa24f087..186e230dc 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs @@ -66,23 +66,6 @@ namespace Tango.MachineStudio.UI SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); - if (!ViewModelBase.IsInDesignModeStatic) - { - LogManager.Categories.Clear(); - LogManager.Categories.AddRange(SettingsManager.Default.MachineStudio.LoggingCategories); - - if (LogManager.Categories.Count == 0) - { - LogManager.Categories.Add(LogCategory.Critical); - LogManager.Categories.Add(LogCategory.Error); - LogManager.Categories.Add(LogCategory.General); - LogManager.Categories.Add(LogCategory.Warning); - } - - LogManager.RegisterLogger(new VSOutputLogger()); - LogManager.RegisterLogger(new FileLogger()); - } - //Register View (Supervising Controller Pattern). if (!ViewModelBase.IsInDesignModeStatic) { diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionResolutions.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionResolutions.cs new file mode 100644 index 000000000..2e7327559 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionResolutions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.MachineStudio.UI.Windows +{ + public enum ExceptionResolutions + { + Shutdown, + Restart, + Ignore + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml new file mode 100644 index 000000000..c08a08842 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Machine Studio encountered an unexpected error. It is recommended to restart the application in order to resolve the issue. + + + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml.cs new file mode 100644 index 000000000..0f74fee17 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionWindow.xaml.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; +using Tango.Core.Commands; +using Tango.MachineStudio.UI.ViewModels; + +namespace Tango.MachineStudio.UI.Windows +{ + /// + /// Interaction logic for ExceptionWindow.xaml + /// + public partial class ExceptionWindow : Window + { + public ExceptionResolutions Resolution { get; set; } + + public String Exception { get; set; } + + public RelayCommand ResolveCommand { get; set; } + + public ExceptionWindow() + { + InitializeComponent(); + DataContext = this; + } + + public ExceptionWindow(Exception ex) : this() + { + Exception = ex.FlattenException(); + ResolveCommand = new RelayCommand(Resolve); + } + + private void Resolve(ExceptionResolutions resolution) + { + Resolution = resolution; + Close(); + } + } +} -- cgit v1.3.1