Short version
When I add my TelemetryInitializer to my TelemetryConfiguration I sometimes get an ArrayTypeMismatchException. What can I do to make sure I can always add my TelemetryInitializer?
Long version
I have a big WPF application that uses an AssemblyResolver to load dependant assemblies as described here: https://stackoverflow.com/a/33977134/1022179. Both my ApplicationInsights dll:s and the assembly where my custom TelemetryInitializer is defined are placed in a subdirectory.
For some users at some times I get an ArrayTypeMismatchException when trying to call LogEventAI in the code below (slightly simplified), but usually it works. My LogHandler is located in an assembly referenced directly by the WPF program. The TelemetryInitializer is located in a different assembly and is dynamically loaded at runtime since the LogHandler assembly depends on it.
Slightly simplified version of my LogHandler:
public class LogHandler
{
private static TelemetryClient _telemetryClient;
private static void EnsureTelemetryClientInitialized()
{
if (_telemetryClient != null)
{
return;
}
var instrumentationKey = Application.Settings.GetSetting("InstrumentationKey");
TelemetryConfiguration configuration = new TelemetryConfiguration(instrumentationKey);
configuration.TelemetryInitializers.Add(new PropertyMyModuleFromStackTraceInitializer());
_telemetryClient = new TelemetryClient(configuration);
}
private static void LogExceptionAI(Exception ex, CustomLogPropertiesClient properties)
{
EnsureTelemetryClientInitialized();
_telemetryClient.TrackException(ex, properties.ConvertToDictionary());
}
}
My TelemetryInitializer is implemented in a different assembly and looks like this:
public class PropertyMyModuleFromStackTraceInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ISupportProperties telemetryWithProperties)
{
if (telemetryWithProperties.Properties.ContainsKey("MY.Module")) return;
var sc = new StackTrace();
var module = MyModuleNameFinder.GetSystemFromExceptionText(sc.ToString()); // This is just a bunch of RegEx examining the stack trace.
telemetryWithProperties.Properties.Add("MY.Module", module);
}
}
}
And this is the stack trace from my log:
MY.Shared.Library.Exceptions.BaseException: Server: **************, User: ********, Sender: TryLogExceptionToAppInsight
--->System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
at System.Collections.Generic.List`1.Add(T item)
at Microsoft.ApplicationInsights.Extensibility.Implementation.SnapshottingCollection`2.Add(TItem item)
at MY.Shared.Library.Logging.LogHandler.EnsureTelemetryClientInitialized()
atMY.Shared.Library.Logging.LogHandler.LogExceptionAI(Exception ex, CustomLogPropertiesClient properties)
// The two methods below are used to make the logger backwards compatible:
at MY.Shared.Library.Logging.LogHandler.TryLogExceptionToAppInsight_TempForUserStory5290(Exception ex, Byte[] imageData, Object sender)
at MY.Shared.Library.Logging.LogHandler.TryLogExceptionToAppInsight(Exception ex, Byte[] imageData, Object sender)
How can I get rid of this intermittent bug? I've tried debugging it several times but haven't come across the bug in Visual Studio.