c# - 为什么添加 TelemetryInitializer 会导致间歇性 ArrayTypeMismatchException?
问题描述
精简版
当我将 TelemetryInitializer 添加到 TelemetryConfiguration 时,有时会收到 ArrayTypeMismatchException。我可以做些什么来确保我总是可以添加我的 TelemetryInitializer?
长版
我有一个大型 WPF 应用程序,它使用 AssemblyResolver 加载依赖程序集,如下所述:https ://stackoverflow.com/a/33977134/1022179 。我的 ApplicationInsights dll:s 和定义了我的自定义 TelemetryInitializer 的程序集都放在一个子目录中。
对于某些用户,有时我在下面的代码中尝试调用 LogEventAI 时会收到 ArrayTypeMismatchException(略微简化),但通常它可以工作。我的 LogHandler 位于 WPF 程序直接引用的程序集中。TelemetryInitializer 位于不同的程序集中,并在运行时动态加载,因为 LogHandler 程序集依赖于它。
我的 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());
}
}
我的 TelemetryInitializer 在不同的程序集中实现,如下所示:
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);
}
}
}
这是我日志中的堆栈跟踪:
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)
我怎样才能摆脱这个间歇性的错误?我已经尝试过多次调试,但没有遇到 Visual Studio 中的错误。
解决方案
推荐阅读
- python - 计算python数据框中一列的Z分数
- android - 用于条带集成的 Firebase 计划付款
- python - 相邻列表:尝试使用 while 循环改进此脚本
- spring - 使用 Freemarker 在 Spring 中进行本地化
- python - 没有名为 django 的模块,但安装了 django - miniconda 干扰了路径?
- sql - 如何从具有相同表的外部 Access 数据库中更新表?
- c++ - 如何使用智能指针替换 char*?
- ansible - 保存 Ansible 自定义模块的位置
- asp.net - 如何将数据从 clickOnce 应用程序传回给调用者
- machine-learning - 有条件抽样