Azure 函数在 Azure 门户上显示 0 次执行

Azure function shows 0 executions on azure portal

我是天青新手。我的设置如下。我有一个 IoT 中心、一个事件中心和一个 Azure 函数。 想法是将消息发送到物联网中心,根据特定的消息类型将它们路由到事件中心,使用函数处理这些事件。

我创建了一个示例控制台应用程序,用于将消息从我的机器发送到 IoT 中心。在 Visual Studio (.Net Core) 上创建了一个 azure 函数并将其发布到 azure 函数应用程序。

现在,当我 运行 控制台应用程序项目和函数应用程序项目在一起时,我看到在 IoT 中心接收到的消息(通过查看 Azure 门户),以及函数被触发和接收我发送的消息(通过查看我机器上弹出的控制台,使用函数应用程序)。

我的问题是,当我随后检查 Azure 门户时,我看到函数执行计数为 0,尽管我在本地检查时可以看到该函数正在选取事件。知道为什么会这样吗?

Event received by the function

zero executions shown on azure portal

功能代码以Visual Studio自带模板为准

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task Run([EventHubTrigger("azureiotquickstarteventhub", Connection = "AzureEventHubConnectionString")] EventData[] events, ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                    // Replace these two lines with your processing logic.
                    log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
                    await Task.Yield();
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    exceptions.Add(e);
                }
            }

            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

            if (exceptions.Count > 1)
                throw new AggregateException(exceptions);

            if (exceptions.Count == 1)
                throw exceptions.Single();
        }
    }
}

如果它在本地工作正常,但在 azure portal 中没有执行,那么请检查 azure portal -> 您的函数应用程序配置中是否指定了“AzureEventHubConnectionString”。步骤如下:

导航到 azure 门户 -> 转到您的函数应用程序 -> 配置 -> 应用程序设置,在该页面上,请检查是否指定了“AzureEventHubConnectionString”并且它应该具有正确的值。截图如下:

对了,你最好为你的函数应用开启Application Insights,这样可以给你更好的监控体验。您可以按照下面的屏幕截图启用 Application Insights:

1.In Azure 门户,单击您的函数应用程序实例,如 EventhubTrigger1:

2.Then 单击 Monitor -> 单击 Configure 按钮以启用应用程序洞察:

之后,当我执行该函数时,我可以在 Azure 门户上看到它正在执行: