Skip to content

Commit ffd7849

Browse files
committed
Fixed code suggestions
1 parent 9f00d46 commit ffd7849

File tree

6 files changed

+75
-76
lines changed

6 files changed

+75
-76
lines changed

src/ExceptionVisualizer/ExceptionDebuggerVisualizerProvider.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ namespace ExceptionVisualizer
88
/// <summary>
99
/// Debugger visualizer provider class for <see cref="System.String"/>.
1010
/// </summary>
11+
/// <remarks>
12+
/// Initializes a new instance of the <see cref="ExceptionDebuggerVisualizerProvider"/> class.
13+
/// </remarks>
14+
/// <param name="extension">Extension instance.</param>
15+
/// <param name="extensibility">Extensibility object.</param>
1116
[VisualStudioContribution]
12-
internal class ExceptionDebuggerVisualizerProvider : DebuggerVisualizerProvider
17+
internal class ExceptionDebuggerVisualizerProvider(ExtensionEntrypoint extension, VisualStudioExtensibility extensibility) : DebuggerVisualizerProvider(extension, extensibility)
1318
{
14-
/// <summary>
15-
/// Initializes a new instance of the <see cref="ExceptionDebuggerVisualizerProvider"/> class.
16-
/// </summary>
17-
/// <param name="extension">Extension instance.</param>
18-
/// <param name="extensibility">Extensibility object.</param>
19-
public ExceptionDebuggerVisualizerProvider(ExtensionEntrypoint extension, VisualStudioExtensibility extensibility)
20-
: base(extension, extensibility)
21-
{
22-
}
2319

2420
/// <inheritdoc/>
2521
public override DebuggerVisualizerProviderConfiguration DebuggerVisualizerProviderConfiguration => new("Exception Visualizer", typeof(Exception))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using ExceptionVisualizer.Models;
2+
using ExceptionVisualizerSource;
3+
using System.Collections.ObjectModel;
4+
using System.Windows;
5+
6+
namespace ExceptionVisualizer
7+
{
8+
internal static class ExceptionModelExtensions
9+
{
10+
internal static ExceptionViewModel ToViewModel(this ExceptionModel exception)
11+
{
12+
var viewModel = new ExceptionViewModel
13+
{
14+
Data = new ObservableCollection<DataViewModel>(exception.Data.Select(d => new DataViewModel
15+
{
16+
Key = d.Key,
17+
Value = d.Value,
18+
})),
19+
Properties = new ObservableCollection<DataViewModel>(exception.Properties.Select(d => new DataViewModel
20+
{
21+
Key = d.Key,
22+
Value = d.Value,
23+
})),
24+
ShowData = exception.Data.Count > 0 ? Visibility.Visible : Visibility.Collapsed,
25+
ShowProperties = exception.Properties.Count > 0 ? Visibility.Visible : Visibility.Collapsed,
26+
HelpLink = exception.HelpLink,
27+
HResult = exception.HResult,
28+
Message = exception.Message,
29+
Source = exception.Source,
30+
StackTrace = exception.StackTrace,
31+
TargetSite = exception.TargetSite,
32+
Demystified = exception.Demystified,
33+
@Type = exception.Type,
34+
};
35+
foreach (var inner in exception.InnerExceptions)
36+
{
37+
viewModel.InnerExceptions.Add(ToViewModel(inner));
38+
}
39+
40+
return viewModel;
41+
}
42+
}
43+
}

src/ExceptionVisualizer/ExceptionUserControl.cs

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77

88
namespace ExceptionVisualizer
99
{
10-
internal class ExceptionUserControl : RemoteUserControl
10+
internal class ExceptionUserControl(VisualizerTarget visualizerTarget) : RemoteUserControl(new ViewModel())
1111
{
12-
private readonly VisualizerTarget visualizerTarget;
12+
private readonly VisualizerTarget visualizerTarget = visualizerTarget;
1313

14-
public ExceptionUserControl(VisualizerTarget visualizerTarget) : base(new ViewModel())
15-
{
16-
this.visualizerTarget = visualizerTarget;
17-
}
18-
19-
private ViewModel ViewModel => (ViewModel)this.DataContext!;
14+
private ViewModel ViewModel => (ViewModel)DataContext!;
2015

2116

2217
public override Task ControlLoadedAsync(CancellationToken cancellationToken)
@@ -28,7 +23,7 @@ public override Task ControlLoadedAsync(CancellationToken cancellationToken)
2823
ExceptionModel? exception = await this.visualizerTarget.ObjectSource.RequestDataAsync<ExceptionModel?>(jsonSerializer: null, CancellationToken.None);
2924
if (exception != null)
3025
{
31-
var viewModel = ToViewModel(exception);
26+
var viewModel = exception.ToViewModel();
3227
ViewModel.Exceptions.Add(viewModel);
3328
Subscribe(viewModel);
3429
viewModel.IsSelected = true;
@@ -43,7 +38,7 @@ public override Task ControlLoadedAsync(CancellationToken cancellationToken)
4338
MessageBox.Show($"ExceptionVisualizer failed with exception:\n{ex}");
4439
Telemetry.TrackException(ex);
4540
}
46-
});
41+
}, cancellationToken);
4742
return Task.CompletedTask;
4843
}
4944

@@ -56,43 +51,9 @@ private void Subscribe(ExceptionViewModel model)
5651
}
5752
}
5853

59-
private ExceptionViewModel ToViewModel(ExceptionModel exception)
60-
{
61-
var viewModel = new ExceptionViewModel
62-
{
63-
Data = new ObservableCollection<DataViewModel>(exception.Data.Select(d => new DataViewModel
64-
{
65-
Key = d.Key,
66-
Value = d.Value,
67-
})),
68-
Properties = new ObservableCollection<DataViewModel>(exception.Properties.Select(d => new DataViewModel
69-
{
70-
Key = d.Key,
71-
Value = d.Value,
72-
})),
73-
ShowData = exception.Data.Count > 0 ? Visibility.Visible : Visibility.Collapsed,
74-
ShowProperties = exception.Properties.Count > 0 ? Visibility.Visible : Visibility.Collapsed,
75-
HelpLink = exception.HelpLink,
76-
HResult = exception.HResult,
77-
Message = exception.Message,
78-
Source = exception.Source,
79-
StackTrace = exception.StackTrace,
80-
TargetSite = exception.TargetSite,
81-
Demystified = exception.Demystified,
82-
@Type = exception.Type,
83-
};
84-
foreach (var inner in exception.InnerExceptions)
85-
{
86-
viewModel.InnerExceptions.Add(ToViewModel(inner));
87-
}
88-
89-
return viewModel;
90-
}
91-
9254
private void Exception_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
9355
{
94-
var model = sender as ExceptionViewModel;
95-
if (model == null) return;
56+
if (sender is not ExceptionViewModel model) return;
9657

9758
if (model.IsSelected)
9859
{

src/ExceptionVisualizer/Telemetry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ExceptionVisualizer
1010
/// </summary>
1111
public static class Telemetry
1212
{
13-
private static IElmahioAPI elmahIoClient;
13+
private static IElmahioAPI? elmahIoClient;
1414

1515
public static bool Enabled { get; set; }
1616

@@ -49,10 +49,10 @@ public static void TrackException(Exception ex)
4949
User = WindowsIdentity.GetCurrent().Name,
5050
Hostname = Hostname(),
5151
Application = "Exception Visualizer",
52-
ServerVariables = new List<Item>
53-
{
52+
ServerVariables =
53+
[
5454
new Item("User-Agent", $"X-ELMAHIO-APPLICATION; OS=Windows; OSVERSION={Environment.OSVersion.Version}; ENGINE=VisualStudio"),
55-
}
55+
]
5656
};
5757

5858
elmahIoClient.Messages.CreateAndNotify(new Guid("ece7db40-a2ea-41f7-838f-9ac9c5514d18"), createMessage);
@@ -75,7 +75,7 @@ private static List<Item> PropertiesToData(Exception exception)
7575
return items;
7676
}
7777

78-
private static string Hostname()
78+
private static string? Hostname()
7979
{
8080
var machineName = Environment.MachineName;
8181
if (!string.IsNullOrWhiteSpace(machineName)) return machineName;

src/ExceptionVisualizerSource/ExceptionModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Runtime.Serialization;
43

54
namespace ExceptionVisualizerSource
@@ -20,13 +19,13 @@ public class ExceptionModel
2019
public string Demystified { get; set; }
2120

2221
[DataMember]
23-
public List<KeyValuePair<string, string>> Data { get; set; } = new List<KeyValuePair<string, string>>();
22+
public List<KeyValuePair<string, string>> Data { get; set; } = [];
2423

2524
[DataMember]
26-
public List<KeyValuePair<string, string>> Properties { get; set; } = new List<KeyValuePair<string, string>>();
25+
public List<KeyValuePair<string, string>> Properties { get; set; } = [];
2726

2827
[DataMember]
29-
public List<ExceptionModel> InnerExceptions { get; set; } = new List<ExceptionModel>();
28+
public List<ExceptionModel> InnerExceptions { get; set; } = [];
3029

3130
[DataMember]
3231
public string Source { get; internal set; }

src/ExceptionVisualizerSource/ExceptionModelSource.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ private static ExceptionModel Convert(Exception e)
3939
Source = e.Source,
4040
StackTrace = e.StackTrace,
4141
TargetSite = e.TargetSite?.ToString(),
42-
};
43-
model.Data = new List<KeyValuePair<string, string>>(e
42+
Data = new List<KeyValuePair<string, string>>(e
4443
.Data
4544
.Keys
4645
.Cast<object>()
4746
.Where(k => !string.IsNullOrWhiteSpace(k.ToString()))
48-
.Select(i => new KeyValuePair<string, string>(i.ToString(), Value(e.Data, i))));
49-
model.Properties = e
50-
.GetType()
51-
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
52-
.Where(p => p.CanRead)
53-
.Where(p => p.PropertyType.IsSubclassOf(typeof(ValueType)) || p.PropertyType.Equals(typeof(string)))
54-
.Where(p => p.Name != "Message" && p.Name != "Source" && p.Name != "HResult" && p.Name != "HelpLink" && p.Name != "TargetSite" && p.Name != "StackTrace" && p.GetValue(e) != null)
55-
.Select(p => new KeyValuePair<string, string>(p.Name, p.GetValue(e)?.ToString()))
56-
.ToList();
47+
.Select(i => new KeyValuePair<string, string>(i.ToString(), Value(e.Data, i)))),
48+
Properties = e
49+
.GetType()
50+
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
51+
.Where(p => p.CanRead)
52+
.Where(p => p.PropertyType.IsSubclassOf(typeof(ValueType)) || p.PropertyType.Equals(typeof(string)))
53+
.Where(p => p.Name != "Message" && p.Name != "Source" && p.Name != "HResult" && p.Name != "HelpLink" && p.Name != "TargetSite" && p.Name != "StackTrace" && p.GetValue(e) != null)
54+
.Select(p => new KeyValuePair<string, string>(p.Name, p.GetValue(e)?.ToString()))
55+
.ToList()
56+
};
5757
var stackTrace = new EnhancedStackTrace(e);
5858
if (stackTrace.FrameCount > 0)
5959
{

0 commit comments

Comments
 (0)