You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function定时触发致Application Insights重复报错的优化方案咨询

解决Azure Function重复错误日志的最佳实践

一、用好Application Insights的智能分组与自定义维度

  • Application Insights自带的智能错误分组会自动把同类型、同调用栈的错误归为一组,但如果需要按业务维度(比如特定实体ID)精准分组,记录错误时加上自定义维度:
    var telemetry = new ExceptionTelemetry(ex);
    telemetry.Properties["EntityId"] = entityId.ToString();
    telemetryClient.TrackException(telemetry);
    
    之后在AI的「失败」面板里,系统会结合异常类型、调用栈和自定义维度自动聚合分组,直接看分组视图就能掌握唯一错误实例,不用逐条翻重复日志。
  • 也可以用Kusto查询聚合重复错误,快速统计每个唯一错误的触发次数:
    exceptions
    | summarize Count = count() by problemId, customDimensions.EntityId, type
    | sort by Count desc
    

二、用Azure持久化存储做错误去重

  • 别用内存Singleton字典(Function缩容或重启会丢状态),改用Azure原生持久化存储记录已出现的错误标识:
    • Azure Table存储:把错误类型+业务键(比如EntityId)分别作为PartitionKey和RowKey,每次触发先查表,不存在才记录错误并写入。
    • Azure Redis缓存:给错误设置带过期时间的键(比如error:NullReferenceException:Entity2),过期时间设得比触发器周期长(比如5分钟),避免短时间内重复记录,这种方式性能更高,适合高频场景。

三、配置Function重试策略与错误过滤

  • 在host.json里配置重试策略,同时在代码里判断是否为首次执行,只在首次错误时记录日志:
    {
      "version": "2.0",
      "extensions": {
        "timers": {
          "retry": {
            "strategy": "fixedDelay",
            "maxRetryCount": 3,
            "delay": "00:00:10"
          }
        }
      }
    }
    
    利用Function执行上下文的重试标识,跳过重试阶段的错误日志记录,避免同一错误多次上报。

四、在Application Insights管道层面过滤重复错误

  • 自定义采样处理器,在Telemetry管道里拦截重复错误,只记录首次出现的实例:
    public class DuplicateErrorSamplingProcessor : ITelemetryProcessor
    {
      private readonly ITelemetryProcessor _nextProcessor;
      private readonly IDistributedCache _cache;
    
      public DuplicateErrorSamplingProcessor(ITelemetryProcessor nextProcessor, IDistributedCache cache)
      {
        _nextProcessor = nextProcessor;
        _cache = cache;
      }
    
      public async void Process(ITelemetry telemetry)
      {
        if (telemetry is ExceptionTelemetry exceptionTelemetry)
        {
          var errorKey = $"{exceptionTelemetry.Exception.GetType().Name}:{exceptionTelemetry.Properties["EntityId"]}";
          if (!await _cache.GetStringAsync(errorKey))
          {
            await _cache.SetStringAsync(errorKey, "logged", new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) });
            _nextProcessor.Process(telemetry);
          }
        }
        else
        {
          _nextProcessor.Process(telemetry);
        }
      }
    }
    
    这种方式不用修改业务代码,直接在日志管道层面做过滤。

内容的提问来源于stack exchange,提问作者Mykyta Halchenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:05:07