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

Azure Functions中ILogger日志未在Application Insights显示的问题求助

解决方案:Azure Functions孤立进程ILogger日志未同步到Application Insights

以下是针对问题的具体排查和修复步骤:

1. 修正host.json的日志配置

孤立进程模式下,需要显式配置Application Insights的日志采集规则,默认不会自动捕获所有ILogger日志。修改你的host.json,添加applicationInsights节点:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      // 可选:明确指定你的函数类别的日志级别,确保不被过滤
      "serviceBus.QueueTrigger": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      // 关键配置:指定Application Insights要采集的日志级别
      "logLevel": {
        "Default": "Information"
      }
    }
  }
}

2. 验证Application Insights连接配置

确保你的Function App已正确配置Application Insights连接字符串:

  • 本地开发:在local.settings.json中添加APPLICATIONINSIGHTS_CONNECTION_STRING,值为你的AI资源连接字符串
  • 云端部署:在Azure门户的Function App → 配置 → 应用设置中,确认APPLICATIONINSIGHTS_CONNECTION_STRING已正确设置且关联到目标AI资源

3. 检查采样规则是否过滤日志

如果高流量场景下采样可能会丢弃部分日志,可临时关闭采样测试:
在host.json的applicationInsights.samplingSettings中设置"isEnabled": false,触发函数后查看AI日志是否出现。若恢复正常,再根据业务需求调整采样比例(比如设置"fixedRate": 1.0确保100%采集)。

4. 确认Program.cs配置正确性

你的Program.cs配置已经包含了孤立进程所需的AI服务注册,确保顺序正确即可:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        services.Configure<JsonSerializerOptions>(options =>
        {
            options.Converters.Add(new JsonStringEnumConverter());
            options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        });
        // 确保这两个AI注册语句顺序正确
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .ConfigureAppConfiguration((context, config) =>
    {
        config.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
        config.AddEnvironmentVariables();
    })
    .Build();

host.Run();

5. 替代日志导出方案

如果以上配置仍未解决问题,可考虑以下备选方案:

  • 使用Azure Monitor日志工作区:将Function App日志发送到Log Analytics工作区,通过更灵活的查询语句获取所有日志(包括ILogger和Console输出)
  • 导出到存储账户:在Function App的监控 → 日志流中配置连续导出,将日志持久化到Blob存储或ADLS Gen2,方便后续分析

内容的提问来源于stack exchange,提问作者Dhanush S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 21:32:29