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

.NET 6 Azure Functions使用VS2022默认代码时日志功能失效

.NET 6 Azure Functions Service Bus触发器日志不显示问题

问题说明

使用Visual Studio 2022默认模板创建的.NET 6 Azure Functions,在Service Bus触发器执行时,控制台输出中看不到ILogger.LogInformation记录的消息日志。

相关代码与输出

host.json

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

函数代码

public class SBFuncTrigger
{
    private readonly ILogger<Function1> _logger;

    public Function1(ILogger<Function1> log)
    {
        _logger = log;
    }

    [FunctionName("SBFuncTrigger")]
    public void Run([ServiceBusTrigger("sampletopic", "sbtopicsubscription", Connection = "ServiceBusConnStr")]string mySbMsg)
    {
        _logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
    }
}

当前控制台输出

For detailed output, run func with --verbose flag.
 Host lock lease acquired by instance ID '<Some_Id>'.
 Executing 'SBFuncTrigger' (Reason='(null)', Id=
<Some_Id>)
 Trigger Details: MessageId: 
<Some_Id>, SequenceNumber: 8, DeliveryCount: 1, EnqueuedTimeUtc: , LockedUntilUtc: , SessionId: (null)
 Executed 'Function1' (Succeeded, Id=
<Some_Id>, Duration=xxxms)

预期未出现的日志内容

C# ServiceBus topic trigger function processed message: {mySbMsg}

实现背景

  • 通过Logic App向Service Bus主题订阅发送内容为Hello to Azure Functions Service Bus的消息
  • 在Azure门户的服务总线命名空间中创建了对应的主题和订阅,以此为基础创建Service Bus触发器函数
  • 已配置Logic App工作流与Service Bus主题订阅的连接
  • 服务总线连接字符串ServiceBusConnStr已在local.settings.json中正确定义

解决方法

1. 修正日志注入的泛型类型不匹配问题

函数类名为SBFuncTrigger,但构造函数中注入的是ILogger<Function1>,类型不匹配会导致日志无法正确输出。修改为对应类的日志实例:

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

    public SBFuncTrigger(ILogger<SBFuncTrigger> log)
    {
        _logger = log;
    }

    [FunctionName("SBFuncTrigger")]
    public void Run([ServiceBusTrigger("sampletopic", "sbtopicsubscription", Connection = "ServiceBusConnStr")]string mySbMsg)
    {
        _logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
    }
}

2. 调整host.json的控制台日志级别配置

默认配置仅设置了Application Insights的采样规则,未明确控制台日志的输出级别。添加控制台日志配置,确保Information级别日志能输出:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        },
        "console": {
            "logLevel": {
                "default": "Information"
            }
        }
    }
}

3. 本地运行时启用详细日志模式

启动函数时添加--verbose参数,强制输出更详细的日志内容:

func start --verbose

4. 检查local.settings.json的日志配置

确保local.settings.json中未覆盖默认日志级别:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "ServiceBusConnStr": "你的服务总线连接字符串"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 12:53:19