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

.NET 8 Isolated Azure Functions升级后无法显示Info及以下级别日志

问题:.NET 8独立工作进程模型下Azure Functions本地日志级别不生效

将Azure Functions应用从.NET 6进程内模型升级到.NET 8独立工作进程模型后,本地控制台仅能显示Error和Warning级别的日志,Information及以下级别日志无法输出。日志代码及原host.json配置与.NET 6时期一致,且修改host.json中对应命名空间的日志级别后无效果。

代码示例:

Logger?.LogError("Test if we can log error");
Logger?.LogWarning("Test if we can log warning");
Logger?.LogInformation("Test if we can log info");
Logger?.LogDebug("Test if we can log debug");

实际输出:

[2024-05-30T10:24:45.823Z] Test if we can log error
[2024-05-30T10:24:45.824Z] Test if we can log warning
.. that's it

原host.json配置:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "AlarmFunctions.Functions": "Debug",
      "DataLayer": "Debug",
      "ServiceLayer": "Debug",
      "Function": "Warning",
      "default": "Warning"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Trace;Request"
      },
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

解决方案

问题根源在于.NET 8独立工作进程模型的日志配置逻辑与.NET 6进程内模型存在差异,控制台日志的过滤规则需要单独配置,而非仅依赖全局logLevel。

  1. 修改host.json,添加控制台日志级别配置
    在logging节点下新增console配置,显式指定控制台日志的级别过滤规则,确保自定义命名空间的日志能输出到控制台:
{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "AlarmFunctions.Functions": "Debug",
      "DataLayer": "Debug",
      "ServiceLayer": "Debug",
      "Function": "Warning",
      "default": "Warning"
    },
    "console": {
      "logLevel": {
        "AlarmFunctions.Functions": "Debug",
        "default": "Warning"
      }
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Trace;Request"
      },
      "logLevel": {
        "default": "Warning"
      }
    }
  }
}

注意:将applicationInsights下的LogLevel改为小写logLevel,保持配置键的一致性。

  1. 检查Program.cs中的主机日志配置
    独立工作进程模型需要手动构建主机,确保已添加控制台日志提供器,并正确加载host.json配置:
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(builder =>
    {
        builder.ConfigureLogging(logging =>
        {
            // 确保控制台日志提供器被注册
            logging.AddConsole();
            // 设置日志的最低级别,优先读取host.json中的配置
            logging.SetMinimumLevel(LogLevel.Debug);
        });
    })
    .Build();

host.Run();
  1. 验证命名空间匹配
    确认日志代码所在的命名空间与host.json中配置的AlarmFunctions.Functions完全一致(注意大小写、层级结构),避免因命名空间不匹配导致过滤规则不生效。

完成以上配置后,重启本地函数主机,即可在控制台看到Information、Debug级别的日志输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 02:18:20