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

Azure函数Trace/Debug/Information日志无法写入Application Insights求助

解决Azure函数低级别日志无法传入Application Insights的问题

1. 补全host.json的Application Insights日志级别配置

仅设置根日志级别不足以控制Application Insights的日志收集,必须单独为其指定过滤规则。确保你的host.json包含以下关键配置:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      // 单独控制传入AI的日志级别,必须明确设置
      "logLevel": {
        "default": "Information"
      }
    },
    "logLevel": {
      "default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

若缺失applicationInsights.logLevel节点,Application Insights会默认只收集Warning及以上级别的日志。

2. 检查采样策略是否过滤低级别日志

采样设置可能会丢弃大量低级别日志以控制流量。可先关闭采样验证:
在samplingSettings中设置"isEnabled": false,部署后查看AI是否能接收全级别日志。若恢复正常,再调整采样参数(比如设置maxTelemetryItemsPerSecond为合理值,或通过includedTypes指定保留的日志类型)。

3. 确认函数应用的AI连接配置

  • 检查函数应用配置项中,APPINSIGHTS_INSTRUMENTATIONKEY是否正确,且未被其他环境变量覆盖。
  • 确保函数运行时版本(如.NET 6/7)与使用的日志包版本兼容,版本不匹配可能导致日志上报失败。

4. 验证日志对象的注入方式

必须通过依赖注入获取ILogger实例,手动创建的日志对象无法被AI捕获。正确使用方式示例:

public class MyTimerFunction
{
    private readonly ILogger<MyTimerFunction> _logger;

    // 通过构造函数注入ILogger
    public MyTimerFunction(ILogger<MyTimerFunction> logger)
    {
        _logger = logger;
    }

    [FunctionName("MyTimerFunction")]
    public void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer)
    {
        _logger.LogTrace("Trace级日志");
        _logger.LogDebug("Debug级日志");
        _logger.LogInformation("Information级日志");
    }
}

5. 调整AI日志查询语句

有时日志已上报,但查询过滤条件过严导致无法看到。在AI日志查询界面使用以下语句查看全级别日志:

traces
| where severityLevel <= 2 // 0=Verbose(Trace),1=Information,2=Warning,3=Error,4=Critical
| order by timestamp desc

内容的提问来源于stack exchange,提问作者Mark C

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 08:52:16