azure - 使用 Visual Studio 2022 提供的默认代码,日志记录在 .NET 6 Azure Functions 中不起作用

标签 azure azure-functions azure-logic-apps azureservicebus asp.net-core-6.0

代码片段:

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}

详细信息/实现:

  1. 创建了逻辑应用,用于将消息发送到主题订阅,内容为“Hello to Azure Functions Service Bus” enter image description here

  2. Azure Functions 服务总线主题触发器 - 使用 Azure 门户服务总线命名空间中的主题和订阅创建。

  3. 逻辑应用工作流连接到服务总线主题订阅以进行消息传输。

  4. 服务总线的连接字符串ServiceBusConnStrlocal.settings.json中定义

最佳答案

您提供的服务总线触发器的函数声明中缺少 ILogger 类。

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace KrishSBNet6FunctionApp
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;
        public Function1(ILogger<Function1> log)
        {
            _logger = log;
        }

        [FunctionName("Function1")]
        public void Run([ServiceBusTrigger("messagetopic", "topicsubsn", Connection = "SBConnection")] string mySbMsg, ILogger _logger)
        {
            _logger.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        }
    }
}

enter image description here

关于azure - 使用 Visual Studio 2022 提供的默认代码,日志记录在 .NET 6 Azure Functions 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75830388/

相关文章:

c# - Azure 使用 Azure Function 保护 WCF asmx

azure - 如何从Http触发函数发送消息到消息总线?

azure - 如何以编程方式访问 Azure Functions 使用指标?

azure - Azure 函数中标识符 'Submission#0' 不符合 CLS

azure - 通过 ARM 模板访问控制配置逻辑应用程序

azure - ADFv2 队列时间

ios - 在 azure-storage-ios SDK 中初始化 AZSCloudStorageAccount 时出现未指定错误

postgresql - Azure 函数应用程序在 PostgreSQL pg 调用上失败

azure 逻辑应用: not corrently checking empty check

azure - 在 Azure 门户中哪里可以找到逻辑应用程序的限制事件?