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

.NET 8.0独立模式Function App无法记录Trace日志仅显示Info日志求助

解决方案

1. 修复日志调用的参数错误

你的函数代码中LogTrace调用存在参数误用问题:

// 错误写法:错误传递SeverityLevel参数,不符合方法重载规则
_logger.LogTrace($"FUNKTION HAS RUN TRACE", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);

// 正确写法:LogTrace本身对应Trace级别,无需手动指定SeverityLevel
_logger.LogTrace("FUNKTION HAS RUN TRACE");

LogTrace方法默认级别就是Trace,额外传递SeverityLevel会导致日志级别无法被正确识别。

2. 调整Application Insights采样规则

host.json中启用的采样机制默认会过滤Trace级日志,需添加最低采样级别配置:

{
  "version": "2.0",
  "extensions": {
    "queues": {
      "messageEncoding": "none"
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request",
        "minTelemetrySeverityLevel": "Trace" // 指定采样最低级别为Trace
      },
      "logLevel": {
        "default": "Trace"
      },
      "enableLiveMetricsFilters": true
    },
    "logLevel": {
      "default": "Trace",
      "Function": "Trace"
    }
  }
}

3. 修正Program.cs的日志过滤器配置

移除默认AI过滤器后,需明确设置全局和AI提供者的日志最低级别:

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();

        var contextualization = services.BuildServiceProvider().GetService<IOptions<ExecutionContextOptions>>()?.Value;

        var config = new ConfigMan(contextualization.AppDirectory);
        services.AddSingleton(config);

        var devOpsRepo = new EDWRepo(config.Values.DB_EDW);
        services.AddSingleton<EDWRepo>(devOpsRepo);

    })
    .ConfigureLogging(logging =>
    {
        logging.SetMinimumLevel(LogLevel.Trace);
        // 明确允许AI日志提供者接收Trace及以上级别日志
        logging.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
    })
    .Build();

host.Run();

4. 验证部署配置一致性

确认部署后的host.json文件与本地配置完全一致,避免部署过程中配置被覆盖。

完成以上修改后重新部署Function App,触发队列消息即可在Application Insights中看到Trace级日志。

内容的提问来源于stack exchange,提问作者Thomas Segato

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:00:55