|
1 | | -using ExceptionVisualizer.Models; |
2 | | -using ExceptionVisualizerSource; |
3 | | -using Microsoft.VisualStudio.Extensibility.DebuggerVisualizers; |
4 | | -using Microsoft.VisualStudio.Extensibility.UI; |
5 | | -using System.Collections.ObjectModel; |
6 | | -using System.Windows; |
7 | | - |
8 | | -namespace ExceptionVisualizer |
9 | | -{ |
10 | | - internal class ExceptionUserControl : RemoteUserControl |
11 | | - { |
12 | | - private readonly VisualizerTarget visualizerTarget; |
13 | | - |
14 | | - public ExceptionUserControl(VisualizerTarget visualizerTarget) : base(new ViewModel()) |
15 | | - { |
16 | | - this.visualizerTarget = visualizerTarget; |
17 | | - } |
18 | | - |
19 | | - private ViewModel ViewModel => (ViewModel)this.DataContext!; |
20 | | - |
21 | | - |
22 | | - public override Task ControlLoadedAsync(CancellationToken cancellationToken) |
23 | | - { |
24 | | - _ = Task.Run(async () => |
25 | | - { |
26 | | - try |
27 | | - { |
28 | | - ExceptionModel? exception = await this.visualizerTarget.ObjectSource.RequestDataAsync<ExceptionModel?>(jsonSerializer: null, CancellationToken.None); |
29 | | - if (exception != null) |
30 | | - { |
31 | | - var viewModel = ToViewModel(exception); |
32 | | - ViewModel.Exceptions.Add(viewModel); |
33 | | - Subscribe(viewModel); |
34 | | - viewModel.IsSelected = true; |
35 | | - } |
36 | | - } |
37 | | - catch (StreamJsonRpc.RemoteInvocationException rie) when (rie.Message.Contains("Could not load file or assembly 'Newtonsoft.Json")) |
38 | | - { |
39 | | - MessageBox.Show("There's currently a bug in Visual Studio causing the Exception Visualizer extension to crash when debugging projects containing a reference to Newtonsoft.Json older than version 13. Either upgrade to version 13 of Newtonsoft.Json or follow this issue on GitHub: https://github.com/microsoft/VSExtensibility/issues/248."); |
40 | | - } |
41 | | - catch (Exception ex) |
42 | | - { |
43 | | - Telemetry.TrackException(ex); |
44 | | - } |
45 | | - }); |
46 | | - return Task.CompletedTask; |
47 | | - } |
48 | | - |
49 | | - private void Subscribe(ExceptionViewModel model) |
50 | | - { |
51 | | - model.PropertyChanged += Exception_PropertyChanged; |
52 | | - foreach (var inner in model.InnerExceptions) |
53 | | - { |
54 | | - Subscribe(inner); |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - private ExceptionViewModel ToViewModel(ExceptionModel exception) |
59 | | - { |
60 | | - var viewModel = new ExceptionViewModel |
61 | | - { |
62 | | - Data = new ObservableCollection<DataViewModel>(exception.Data.Select(d => new DataViewModel |
63 | | - { |
64 | | - Key = d.Key, |
65 | | - Value = d.Value, |
66 | | - })), |
67 | | - Properties = new ObservableCollection<DataViewModel>(exception.Properties.Select(d => new DataViewModel |
68 | | - { |
69 | | - Key = d.Key, |
70 | | - Value = d.Value, |
71 | | - })), |
72 | | - ShowData = exception.Data.Count > 0 ? Visibility.Visible : Visibility.Collapsed, |
73 | | - ShowProperties = exception.Properties.Count > 0 ? Visibility.Visible : Visibility.Collapsed, |
74 | | - HelpLink = exception.HelpLink, |
75 | | - HResult = exception.HResult, |
76 | | - Message = exception.Message, |
77 | | - Source = exception.Source, |
78 | | - StackTrace = exception.StackTrace, |
79 | | - TargetSite = exception.TargetSite, |
80 | | - Demystified = exception.Demystified, |
81 | | - @Type = exception.Type, |
82 | | - }; |
83 | | - foreach (var inner in exception.InnerExceptions) |
84 | | - { |
85 | | - viewModel.InnerExceptions.Add(ToViewModel(inner)); |
86 | | - } |
87 | | - |
88 | | - return viewModel; |
89 | | - } |
90 | | - |
91 | | - private void Exception_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) |
92 | | - { |
93 | | - var model = sender as ExceptionViewModel; |
94 | | - if (model == null) return; |
95 | | - |
96 | | - if (model.IsSelected) |
97 | | - { |
98 | | - ViewModel.SelectedItem = model; |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | -} |
| 1 | +using ExceptionVisualizer.Models; |
| 2 | +using ExceptionVisualizerSource; |
| 3 | +using Microsoft.VisualStudio.Extensibility.DebuggerVisualizers; |
| 4 | +using Microsoft.VisualStudio.Extensibility.UI; |
| 5 | +using System.Collections.ObjectModel; |
| 6 | +using System.Windows; |
| 7 | + |
| 8 | +namespace ExceptionVisualizer |
| 9 | +{ |
| 10 | + internal class ExceptionUserControl(VisualizerTarget visualizerTarget) : RemoteUserControl(new ViewModel()) |
| 11 | + { |
| 12 | + private readonly VisualizerTarget visualizerTarget = visualizerTarget; |
| 13 | + |
| 14 | + private ViewModel ViewModel => (ViewModel)DataContext!; |
| 15 | + |
| 16 | + |
| 17 | + public override Task ControlLoadedAsync(CancellationToken cancellationToken) |
| 18 | + { |
| 19 | + _ = Task.Run(async () => |
| 20 | + { |
| 21 | + try |
| 22 | + { |
| 23 | + ExceptionModel? exception = await this.visualizerTarget.ObjectSource.RequestDataAsync<ExceptionModel?>(jsonSerializer: null, CancellationToken.None); |
| 24 | + if (exception != null) |
| 25 | + { |
| 26 | + var viewModel = exception.ToViewModel(); |
| 27 | + ViewModel.Exceptions.Add(viewModel); |
| 28 | + Subscribe(viewModel); |
| 29 | + viewModel.IsSelected = true; |
| 30 | + } |
| 31 | + } |
| 32 | + catch (StreamJsonRpc.RemoteInvocationException rie) when (rie.Message.Contains("Could not load file or assembly 'Newtonsoft.Json")) |
| 33 | + { |
| 34 | + MessageBox.Show("There's currently a bug in Visual Studio causing the Exception Visualizer extension to crash when debugging projects containing a reference to Newtonsoft.Json older than version 13. Either upgrade to version 13 of Newtonsoft.Json or follow this issue on GitHub: https://github.com/microsoft/VSExtensibility/issues/248."); |
| 35 | + } |
| 36 | + catch (Exception ex) |
| 37 | + { |
| 38 | + MessageBox.Show($"ExceptionVisualizer failed with exception:\n{ex}"); |
| 39 | + Telemetry.TrackException(ex); |
| 40 | + } |
| 41 | + }, cancellationToken); |
| 42 | + return Task.CompletedTask; |
| 43 | + } |
| 44 | + |
| 45 | + private void Subscribe(ExceptionViewModel model) |
| 46 | + { |
| 47 | + model.PropertyChanged += Exception_PropertyChanged; |
| 48 | + foreach (var inner in model.InnerExceptions) |
| 49 | + { |
| 50 | + Subscribe(inner); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void Exception_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 55 | + { |
| 56 | + if (sender is not ExceptionViewModel model) return; |
| 57 | + |
| 58 | + if (model.IsSelected) |
| 59 | + { |
| 60 | + ViewModel.SelectedItem = model; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments