diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-12 12:53:46 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-12 12:53:46 +0200 |
| commit | 8231c057a4073e7397dbb1d953c43a76d8187e72 (patch) | |
| tree | 1c40c8497aac2f10f919ab732af0c357493ca320 /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI | |
| parent | 2bef1ef7fb1d5cd57e2af3f47a648e512cfcd4f2 (diff) | |
| download | Tango-8231c057a4073e7397dbb1d953c43a76d8187e72.tar.gz Tango-8231c057a4073e7397dbb1d953c43a76d8187e72.zip | |
Implemented global exception trapping on machine studio.
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI')
7 files changed, 192 insertions, 17 deletions
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 /// </summary> 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 + + /// <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) + { + 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 Binary files differnew file mode 100644 index 000000000..9a4abb703 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Images/exception.png 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 @@ <Compile Include="Views\ShutdownView.xaml.cs"> <DependentUpon>ShutdownView.xaml</DependentUpon> </Compile> + <Compile Include="Windows\ExceptionResolutions.cs" /> + <Compile Include="Windows\ExceptionWindow.xaml.cs"> + <DependentUpon>ExceptionWindow.xaml</DependentUpon> + </Compile> <Page Include="MainWindow.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> @@ -215,6 +219,10 @@ <SubType>Designer</SubType> <Generator>MSBuild:Compile</Generator> </Page> + <Page Include="Windows\ExceptionWindow.xaml"> + <SubType>Designer</SubType> + <Generator>MSBuild:Compile</Generator> + </Page> </ItemGroup> <ItemGroup> <Compile Include="Properties\AssemblyInfo.cs"> @@ -329,6 +337,9 @@ <Resource Include="Images\external-bridge-tcp.png" /> <Resource Include="Images\external-bridge-usb.png" /> </ItemGroup> + <ItemGroup> + <Resource Include="Images\exception.png" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <PropertyGroup> <PostBuildEvent>$(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<MachineConnectionViewVM>(); SimpleIoc.Default.Register<MachineLoginViewVM>(); - 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 @@ +<Window x:Class="Tango.MachineStudio.UI.Windows.ExceptionWindow" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:local="clr-namespace:Tango.MachineStudio.UI.Windows" + mc:Ignorable="d" + WindowStyle="None" ResizeMode="NoResize" Topmost="True" AllowsTransparency="True" WindowStartupLocation="CenterScreen" d:DesignHeight="300" d:DesignWidth="300" Width="610" Height="410" Background="Transparent" d:DataContext="{d:DesignInstance Type=local:ExceptionWindow, IsDesignTimeCreatable=False}"> + <Grid> + <Border BorderThickness="1" BorderBrush="DodgerBlue" Background="White" Margin="10" CornerRadius="10"> + <Border.Effect> + <DropShadowEffect ShadowDepth="0" BlurRadius="10" /> + </Border.Effect> + <Grid> + <Grid.RowDefinitions> + <RowDefinition Height="80"/> + <RowDefinition Height="1*"/> + </Grid.RowDefinitions> + + <Grid> + <StackPanel Orientation="Horizontal" Margin="10"> + <Image Source="/Images/exception.png" RenderOptions.BitmapScalingMode="Fant"></Image> + <TextBlock Text="Machine Studio Error" VerticalAlignment="Center" Margin="10 0 0 0" FontSize="20"></TextBlock> + </StackPanel> + </Grid> + + <Grid Grid.Row="1" Margin="10 0 10 10"> + <Grid.RowDefinitions> + <RowDefinition Height="1*"/> + <RowDefinition Height="50"/> + </Grid.RowDefinitions> + + <Grid> + <Grid.RowDefinitions> + <RowDefinition Height="30"/> + <RowDefinition Height="223*"/> + </Grid.RowDefinitions> + + <Grid> + <TextBlock TextWrapping="Wrap" VerticalAlignment="Center" Margin="0 0 0 0"> + <Run>Machine Studio encountered an unexpected error. It is recommended to restart the application in order to resolve the issue.</Run> + </TextBlock> + </Grid> + + <Grid Grid.Row="1" Margin="0 10 0 0"> + <TextBox Style="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderBrush="#515151" IsReadOnly="True" AcceptsReturn="True" Foreground="#FF5C5C" Padding="2" TextWrapping="Wrap" Text="{Binding Exception}"></TextBox> + </Grid> + </Grid> + + <Grid Grid.Row="1"> + <Button HorizontalAlignment="Left" Width="140" Command="{Binding ResolveCommand}" CommandParameter="{x:Static local:ExceptionResolutions.Ignore}">Ignore</Button> + <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> + <Button Margin="5 0 0 0" Width="140" Command="{Binding ResolveCommand}" CommandParameter="{x:Static local:ExceptionResolutions.Shutdown}">Shutdown</Button> + <Button Margin="5 0 0 0" Width="140" Command="{Binding ResolveCommand}" CommandParameter="{x:Static local:ExceptionResolutions.Restart}">Restart</Button> + </StackPanel> + </Grid> + </Grid> + </Grid> + </Border> + </Grid> +</Window> 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 +{ + /// <summary> + /// Interaction logic for ExceptionWindow.xaml + /// </summary> + public partial class ExceptionWindow : Window + { + public ExceptionResolutions Resolution { get; set; } + + public String Exception { get; set; } + + public RelayCommand<ExceptionResolutions> ResolveCommand { get; set; } + + public ExceptionWindow() + { + InitializeComponent(); + DataContext = this; + } + + public ExceptionWindow(Exception ex) : this() + { + Exception = ex.FlattenException(); + ResolveCommand = new RelayCommand<ExceptionResolutions>(Resolve); + } + + private void Resolve(ExceptionResolutions resolution) + { + Resolution = resolution; + Close(); + } + } +} |
