如何解决Application Insights遥测处理器未捕获异常
我有一个自定义遥测处理器,它将自定义异常字段发送到Application Insights。
例如,如果我抛出这样的自定义异常
class MyException : Exception
{
public string AdditionalData { get; set; }
}
然后将AdditionalData
作为自定义应用洞察字段发送。
public class CustomExceptionDataTelemetryProcessor : ITelemetryProcessor
{
/// <summary>
/// Default exception fields that should be ignore because they are part of logs by default
/// </summary>
private static IEnumerable<string> _DefaultExceptionFields = new Exception().GetType().GetProperties().Select(e => e.Name);
private readonly ITelemetryProcessor _next;
public CustomExceptionDataTelemetryProcessor(ITelemetryProcessor next)
{
_next = next;
}
public void Process(ITelemetry item)
{
ModifyItem(item);
_next.Process(item);
}
/// <summary>
/// Adds exception fields as custom data
/// </summary>
private void ModifyItem(ITelemetry item)
{
if (!(item is ExceptionTelemetry exceptionTelementry))
{
return;
}
var exception = exceptionTelementry.Exception;
var exceptionType = exception.GetType();
foreach (var property in exceptionType.GetProperties())
{
// ignore default fields
if (_DefaultExceptionFields.Contains(property.Name))
{
continue;
}
var value = property.GetValue(exception);
var serializedValue = (value is string) ? value.ToString() : HttpClientUtils.Serialize(value);
exceptionTelementry.Properties[property.Name] = serializedValue;
}
}
}
我这样注册该处理器
var telemetryBuilder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
telemetryBuilder.Use((next) => new CustomExceptionDataTelemetryProcessor(next));
telemetryBuilder.Build();
在Microsoft.ApplicationInsights.AspNetCore
版2.2.1中,一切正常。
如果我升级到最新版本2.15.0,它仍然可以正常工作,但是我收到警告,提示已弃用注册码,因此我根据documentation
进行了更改。services.AddApplicationInsightsTelemetryProcessor<CustomExceptionDataTelemetryProcessor>();
但是现在该异常不再被处理器“捕获”。仅RequestTelemetry
或DependencyTelemetry
类型的项目。没有ExceptionTelemetry
类型的项目。
我可以保留旧的注册方式,但是认为这可能表明某些事情不太正确,并可能导致其他问题。
我尝试在注册自己的所有初始化器和处理器之前将其删除,但这无济于事。其他可能会过滤掉异常。
services.RemoveAll(typeof(ITelemetryInitializer));
services.RemoveAll(typeof(ITelemetryProcessor));
解决方法
它与Microsoft.ApplicationInsights.AspNetCore
的{{3}}版一起使用
我还安装了Microsoft.ApplicationInsights
的NuGet软件包,并带有其他版本。将其删除并仅保留Microsoft.ApplicationInsights.AspNetCore
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。