如何从 .NET Core 2.1 的 Application Insights 采样中排除异常和错误日志记录?
How to exclude Exception and Error logging from sampling in Application Insights in .NET Core 2.1?
我是 运行 网络 api,具有以下规格:
- ASP.NET 核心 2.1
- Microsoft.ApplicationInsights.AspNetCore 2.13.1
- 托管在 Azure 应用服务中。
在 Startup.cs ConfigureServices 我添加了:
services.AddApplicationInsightsTelemetry();
_loggerFactory.AddAzureWebAppDiagnostics();
我在 Startup.cs:
中设置了自定义异常处理程序
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseCustomExceptionHandler(telemetryClient, _loggerFactory);
}
在这个 CustomExceptionHandler 中,我尝试像这样记录异常:
var logger = loggerFactory.CreateLogger("Unhandled Exception");
logger.LogError(ex, errorId);
var telemetryProperties = new Dictionary<string, string>();
telemetryProperties.Add("errorId", errorId);
telemetryProperties.Add("traceIdentifier", context.TraceIdentifier);
telemetryClient.TrackException(ex, properties: telemetryProperties);
使用此配置后,并非所有异常或错误日志都到达 Log Analytics 存储桶。所以我找到了 Application Insights 的这个配置:
var builder = aiTelemetryConfiguration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
builder.UseAdaptiveSampling(excludedTypes: "Trace;Exception");
builder.Build();
这里我从自适应采样中排除了Trace和Exception。
目前此配置已投入生产。它每分钟处理 +/- 50k 个请求。但是异常存储桶保持为空。
我注意到 Trace 之间的这些消息:
AI (Internal): [Microsoft-ApplicationInsights-Core] [msg=Log Error];[msg=Exception while initializing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer, exception message - System.ArgumentException: The key already existed in the dictionary.
at System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary.Add(TKey key, TValue value)
at Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer.Initialize(ITelemetry telemetry)
at Microsoft.ApplicationInsights.TelemetryClient.Initialize(ITelemetry telemetry)]
和
AI: A Metric Extractor detected a telemetry item with SamplingPercentage < 100. Metrics Extractors should be used before Sampling Processors or any other Telemetry Processors that might filter out Telemetry Items. Otherwise, extracted metrics may be incorrect.
明确地说,我正在查看这些位置:
- Azure Application Insights -> 搜索 -> 跟踪|自定义事件|异常
使用此查询进行 Log Analytics:
exceptions
| order by timestamp desc
这至少是禁用采样的正确方法吗?
非常感谢。
我现在添加这个 TelemetryInitializer:
public class ExceptionTelemetrySamplingFilter : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ExceptionTelemetry)
{
((ISupportSampling)telemetry).SamplingPercentage = 100;
}
}
}
并注册:
services.AddSingleton<ITelemetryInitializer, ExceptionTelemetrySamplingFilter>();
来自 official documentation about sampling。我从启动中删除了添加的配置。
我会及时通知您有关改进的信息。
我是 运行 网络 api,具有以下规格:
- ASP.NET 核心 2.1
- Microsoft.ApplicationInsights.AspNetCore 2.13.1
- 托管在 Azure 应用服务中。
在 Startup.cs ConfigureServices 我添加了:
services.AddApplicationInsightsTelemetry();
_loggerFactory.AddAzureWebAppDiagnostics();
我在 Startup.cs:
中设置了自定义异常处理程序if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseCustomExceptionHandler(telemetryClient, _loggerFactory);
}
在这个 CustomExceptionHandler 中,我尝试像这样记录异常:
var logger = loggerFactory.CreateLogger("Unhandled Exception");
logger.LogError(ex, errorId);
var telemetryProperties = new Dictionary<string, string>();
telemetryProperties.Add("errorId", errorId);
telemetryProperties.Add("traceIdentifier", context.TraceIdentifier);
telemetryClient.TrackException(ex, properties: telemetryProperties);
使用此配置后,并非所有异常或错误日志都到达 Log Analytics 存储桶。所以我找到了 Application Insights 的这个配置:
var builder = aiTelemetryConfiguration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
builder.UseAdaptiveSampling(excludedTypes: "Trace;Exception");
builder.Build();
这里我从自适应采样中排除了Trace和Exception。
目前此配置已投入生产。它每分钟处理 +/- 50k 个请求。但是异常存储桶保持为空。
我注意到 Trace 之间的这些消息:
AI (Internal): [Microsoft-ApplicationInsights-Core] [msg=Log Error];[msg=Exception while initializing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer, exception message - System.ArgumentException: The key already existed in the dictionary. at System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary.Add(TKey key, TValue value) at Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer.Initialize(ITelemetry telemetry) at Microsoft.ApplicationInsights.TelemetryClient.Initialize(ITelemetry telemetry)]
和
AI: A Metric Extractor detected a telemetry item with SamplingPercentage < 100. Metrics Extractors should be used before Sampling Processors or any other Telemetry Processors that might filter out Telemetry Items. Otherwise, extracted metrics may be incorrect.
明确地说,我正在查看这些位置:
- Azure Application Insights -> 搜索 -> 跟踪|自定义事件|异常
使用此查询进行 Log Analytics:
exceptions | order by timestamp desc
这至少是禁用采样的正确方法吗?
非常感谢。
我现在添加这个 TelemetryInitializer:
public class ExceptionTelemetrySamplingFilter : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ExceptionTelemetry)
{
((ISupportSampling)telemetry).SamplingPercentage = 100;
}
}
}
并注册:
services.AddSingleton<ITelemetryInitializer, ExceptionTelemetrySamplingFilter>();
来自 official documentation about sampling。我从启动中删除了添加的配置。
我会及时通知您有关改进的信息。