Application Insights 遥测处理器未捕获异常
Application Insights telemetry processor not catching exception
我有一个自定义遥测处理器,可将自定义异常字段发送到 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 it still works fine, but I get a warning that the registration code is deprecated so I changed it per the documentation
services.AddApplicationInsightsTelemetryProcessor<CustomExceptionDataTelemetryProcessor>();
但是现在处理器不再 'caught' 处理异常。只有 RequestTelemetry
或 DependencyTelemetry
类型的项目。没有 ExceptionTelemetry
.
我可以用旧的方式离开注册,但认为这可能表明某些事情不太正确,可能会在其他地方引起其他问题。
我试图在注册自己的之前删除所有初始化程序和处理器,但没有帮助。其他东西可能会过滤掉异常。
services.RemoveAll(typeof(ITelemetryInitializer));
services.RemoveAll(typeof(ITelemetryProcessor));
适用于 Microsoft.ApplicationInsights.AspNetCore
我还 Microsoft.ApplicationInsights
安装了不同版本的 NuGet 包。将其删除并仅保留 Microsoft.ApplicationInsights.AspNetCore
.