我们的 azure 函数中有这个 host.json 文件:
{
"version": "2.0",
"functions": [ "xxx" ],
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
}
}
现在我们希望排除自动记录的异常(因为我们自己在 try catch block 中处理异常,所以不希望它记录两次)。 但是,根据此问题,我不确定抽样排除类型是否是正确使用的属性: https://github.com/MicrosoftDocs/azure-docs/issues/47219 , exceptTypes 是要使用的类型 我应该这样做吗:
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request;Exception"
}
}
}
最佳答案
通常我会说你需要的是 Telemetry Processor或遥测初始化程序,根据语言,删除所有异常遥测。但不幸的是,这在 Azure 函数中不起作用,它是 not supported .
但是,我们可以利用采样设置,通过强制采样遥测数据来防止发送遥测数据。当启用自适应采样时它确实可以工作(我只是没有尝试其他类型的采样),这是 Azure 函数的默认采样行为。为此,我们可以设置 ProactiveSamplingDecision
属性,如下所示:
public class DropExceptionTelemetry : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (!(telemetry is ExceptionTelemetry item)) return;
item.ProactiveSamplingDecision = SamplingDecision.SampledOut;
}
}
另外,不要忘记使用 DI 添加此初始值设定项:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, DropExceptionTelemetry>();
builder.Services.AddLogging();
}
}
要使其正常工作,请确保在 exludedTypes 属性中不监听异常
"applicationInsights": {
"samplingSettings": {
"excludedTypes": "Event;Request"
}
}
关于azure - 如何从azure函数的appinsight中排除异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64355380/