Skip to content

Commit 0abbac6

Browse files
committed
fix: null reference when logging
1 parent d9d7e59 commit 0abbac6

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

src/CodeNav.OutOfProc/Helpers/LogHelper.cs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.ApplicationInsights;
2+
using Microsoft.VisualStudio.ApplicationInsights.Channel;
23
using System.Diagnostics;
34
using System.Reflection;
45
using System.Text.Json;
@@ -12,39 +13,54 @@ public static class LogHelper
1213

1314
public static void GetClient()
1415
{
15-
_client = new TelemetryClient();
16-
_client.Context.Session.Id = Guid.NewGuid().ToString();
17-
_client.InstrumentationKey = InstrumentationKey;
16+
_client = new TelemetryClient(new()
17+
{
18+
InstrumentationKey = InstrumentationKey,
19+
TelemetryChannel = new InMemoryChannel(),
20+
})
21+
{
22+
InstrumentationKey = InstrumentationKey,
23+
};
24+
1825
_client.Context.Component.Version = GetExecutingAssemblyVersion().ToString();
1926
}
2027

2128
public static void Log(string message, Exception? exception = null,
2229
object? additional = null)
2330
{
24-
if (_client == null)
31+
try
2532
{
26-
GetClient();
27-
}
33+
if (_client == null)
34+
{
35+
GetClient();
36+
}
2837

29-
if (_client == null)
30-
{
31-
return;
32-
}
38+
if (_client == null)
39+
{
40+
return;
41+
}
3342

34-
var properties = new Dictionary<string, string>
35-
{
36-
{ "version", GetExecutingAssemblyVersion().ToString() },
37-
{ "message", JsonSerializer.Serialize(message) },
38-
{ "additional", JsonSerializer.Serialize(additional) }
39-
};
43+
var properties = new Dictionary<string, string>
44+
{
45+
{ "version", GetExecutingAssemblyVersion().ToString() },
46+
{ "message", JsonSerializer.Serialize(message) },
47+
{ "additional", JsonSerializer.Serialize(additional) },
48+
};
4049

41-
if (exception == null)
42-
{
43-
_client.TrackEvent(message, properties);
50+
if (exception == null)
51+
{
52+
_client.TrackEvent(message, properties);
53+
}
54+
else
55+
{
56+
_client.TrackException(exception, properties);
57+
}
58+
59+
_client.Flush();
4460
}
45-
else
61+
catch (Exception)
4662
{
47-
_client.TrackException(exception, properties);
63+
// Ignore errors while trying to log errors
4864
}
4965
}
5066

0 commit comments

Comments
 (0)