首页 > 解决方案 > 不知道 Azure 函数是否正在记录任何事件,python 中的本地开发 VS 代码

问题描述

我正在尝试开发一个 Azure 函数来与 IoTHub 交互。我正在 python 版本 3.7.8 中使用 Visual Studio Code 1.47.2 进行本地开发。该函数是第 2 版,使用 Azure Functions Core Tools 2.7.2628。对于开发,我使用的是 Azure Storage Emulator 5.10.0.0

我所做的是使用 EventHub 触发器在 python 中创建一个默认项目。然后修改以下文件:

function.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "event",
      "direction": "in",
      "eventHubName": "IoTHubName", #Got from the Event Hub-compatible name in Build-in endpoints
      "connection": "IoTHubConnectionString", #Got from Event Hub-compatible endpoint
      "cardinality": "many",
      "consumerGroup": "$Default",
      "dataType": "binary"
    }
  ]
}
local.setting.json
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "eventHubConnectionString": "here the Event Hub-compatible endpoint"
  }
}

因为我想首先查看该功能是否有效,所以我对该功能使用了以下代码:

_init_.py
from typing import List
import logging

import azure.functions as func


def main(event: func.EventHubEvent):
    logging.info('Python EventHub trigger processed an event: %s',
                        event.get_body().decode('utf-8'))

我用 F5 开始调试,终端显示如下:

> Executing task: .venv\Scripts\python -m pip install -r requirements.txt <

Requirement already satisfied: azure-functions in c:\...\pythontries\pyfuncappeventhub1\.venv\lib\site-packages (from -r requirements.txt (line 1)) (1.3.0)

Terminal will be reused by tasks, press any key to close it.

> Executing task: .venv\Scripts\activate ; func host start <

Found Python version 3.7.8 (py).

                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

Azure Functions Core Tools (2.7.2628 Commit hash: 42e149f5313330f1f0a5e721a7e59d9ef7d27e6f)
Function Runtime Version: 2.0.13907.0
AZURE_FUNCTIONS_ENVIRONMENT: Development
[22.07.2020 10:07:12] Building host: startup suppressed: 'False', configuration suppressed: 'False', startup operation id: 'somecode'
[22.07.2020 10:07:12] Reading host configuration file 'C:...\host.json'
[22.07.2020 10:07:12] Host configuration file read:
[22.07.2020 10:07:12] {
[22.07.2020 10:07:12]   "version": "2.0",
[22.07.2020 10:07:12]   "logging": {
[22.07.2020 10:07:12]     "applicationInsights": {
[22.07.2020 10:07:12]       "samplingSettings": {
[22.07.2020 10:07:12]         "isEnabled": true
[22.07.2020 10:07:12]       }
[22.07.2020 10:07:12]     }
[22.07.2020 10:07:12]   },
[22.07.2020 10:07:12]   "extensionBundle": {
[22.07.2020 10:07:12]     "id": "Microsoft.Azure.Functions.ExtensionBundle",
[22.07.2020 10:07:12]     "version": "[1.*, 2.0.0)"
[22.07.2020 10:07:12]   }
[22.07.2020 10:07:12] }
[22.07.2020 10:07:12] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:java
[22.07.2020 10:07:12] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:node
[22.07.2020 10:07:12] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:powershell
[22.07.2020 10:07:12] Loading functions metadata
[22.07.2020 10:07:12] Reading functions metadata
[22.07.2020 10:07:12] 1 functions found
[22.07.2020 10:07:12] 1 functions loaded
[22.07.2020 10:07:12] Looking for extension bundle Microsoft.Azure.Functions.ExtensionBundle at C:\...\AppData\Local\Temp\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle
[22.07.2020 10:07:12] Found a matching extension bundle at C:\...\AppData\Local\Temp\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle\1.3.0
[22.07.2020 10:07:12] Fetching information on versions of extension bundle Microsoft.Azure.Functions.ExtensionBundle available on https://functionscdn.azureedge.net/public/ExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/index.json
[22.07.2020 10:07:13] Skipping bundle download since it already exists at path C:\...\AppData\Local\Temp\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle\1.3.0
[22.07.2020 10:07:13] Loading extension bundle from C:\...\AppData\Local\Temp\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle\1.3.0
[22.07.2020 10:07:13] Loading startup extension 'EventHubs'
[22.07.2020 10:07:13] Loaded extension 'EventHubs' (3.0.6.0)
[22.07.2020 10:07:13] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:java
[22.07.2020 10:07:13] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:node
[22.07.2020 10:07:13] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:powershell
[22.07.2020 10:07:13] Initializing Warmup Extension.
[22.07.2020 10:07:13] Initializing Host. OperationId: 'somecode'.
[22.07.2020 10:07:13] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=somecode
[22.07.2020 10:07:13] LoggerFilterOptions
[22.07.2020 10:07:13] {
[22.07.2020 10:07:13]   "MinLevel": "None",
[22.07.2020 10:07:13]   "Rules": [
[22.07.2020 10:07:13]     {
[22.07.2020 10:07:13]       "ProviderName": null,
[22.07.2020 10:07:13]       "CategoryName": null,
[22.07.2020 10:07:13]       "LogLevel": null,
[22.07.2020 10:07:13]       "Filter": "<AddFilter>b__0"
[22.07.2020 10:07:13]     },
[22.07.2020 10:07:13]     {
[22.07.2020 10:07:13]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[22.07.2020 10:07:13]       "CategoryName": null,
[22.07.2020 10:07:13]       "LogLevel": "None",
[22.07.2020 10:07:13]       "Filter": null
[22.07.2020 10:07:13]     },
[22.07.2020 10:07:13]     {
[22.07.2020 10:07:13]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[22.07.2020 10:07:13]       "CategoryName": null,
[22.07.2020 10:07:13]       "LogLevel": null,
[22.07.2020 10:07:13]       "Filter": "<AddFilter>b__0"
[22.07.2020 10:07:13]     }
[22.07.2020 10:07:13]   ]
[22.07.2020 10:07:13] }
[22.07.2020 10:07:13] FunctionResultAggregatorOptions
[22.07.2020 10:07:13] {
[22.07.2020 10:07:13]   "BatchSize": 1000,
[22.07.2020 10:07:13]   "FlushTimeout": "00:00:30",
[22.07.2020 10:07:13]   "IsEnabled": true
[22.07.2020 10:07:13] }
[22.07.2020 10:07:13] SingletonOptions
[22.07.2020 10:07:13] {
[22.07.2020 10:07:13]   "LockPeriod": "00:00:15",
[22.07.2020 10:07:13]   "ListenerLockPeriod": "00:00:15",
[22.07.2020 10:07:13]   "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
[22.07.2020 10:07:13]   "LockAcquisitionPollingInterval": "00:00:05",
[22.07.2020 10:07:13]   "ListenerLockRecoveryPollingInterval": "00:01:00"
[22.07.2020 10:07:13] }
[22.07.2020 10:07:13] HttpOptions
[22.07.2020 10:07:13] {
[22.07.2020 10:07:13]   "DynamicThrottlesEnabled": false,
[22.07.2020 10:07:13]   "MaxConcurrentRequests": -1,
[22.07.2020 10:07:13]   "MaxOutstandingRequests": -1,
[22.07.2020 10:07:13]   "RoutePrefix": "api"
[22.07.2020 10:07:13] }
[22.07.2020 10:07:13] EventHubOptions
[22.07.2020 10:07:13] {
[22.07.2020 10:07:13]   "BatchCheckpointFrequency": 1,
[22.07.2020 10:07:13]   "EventProcessorOptions": {
[22.07.2020 10:07:13]     "EnableReceiverRuntimeMetric": false,
[22.07.2020 10:07:13]     "InvokeProcessorAfterReceiveTimeout": false,
[22.07.2020 10:07:13]     "MaxBatchSize": 10,
[22.07.2020 10:07:13]     "PrefetchCount": 300,
[22.07.2020 10:07:13]     "ReceiveTimeout": "00:01:00"
[22.07.2020 10:07:13]   },
[22.07.2020 10:07:13]   "PartitionManagerOptions": {
[22.07.2020 10:07:13]     "LeaseDuration": "00:00:30",
[22.07.2020 10:07:13]     "RenewInterval": "00:00:10"
[22.07.2020 10:07:13]   }
[22.07.2020 10:07:13] }
[22.07.2020 10:07:13] Starting JobHost
[22.07.2020 10:07:13] Starting Host (HostId=somecode, InstanceId=somecode, Version=2.0.13907.0, ProcessId=somecode, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=(null))
[22.07.2020 10:07:13] Loading functions metadata
[22.07.2020 10:07:13] Loading proxies metadata
[22.07.2020 10:07:13] Initializing Azure Function proxies
[22.07.2020 10:07:14] 0 proxies loaded
[22.07.2020 10:07:14] 1 functions loaded
[22.07.2020 10:07:14] Starting worker process:py  c:/U.../.vscode/extensions/ms-python.python-2020.7.94776/pythonFiles/lib/python/debugpy --listen 127.0.0.1:9091 --wait-for-client "C:\ProgramData\chocolatey\lib\azure-functions-core-tools\tools\workers\python\3.7/WINDOWS/X64/worker.py" --host 127.0.0.1 --port 51794 --workerId somecode --requestId somecode --grpcMaxMessageLength 2147483647
[22.07.2020 10:07:14] py process with Id=19880 started
[22.07.2020 10:07:14] Generating 1 job function(s)
[22.07.2020 10:07:14] Found the following functions:
[22.07.2020 10:07:14] Host.Functions.pyEventHubTrigger1
[22.07.2020 10:07:14]
[22.07.2020 10:07:14] Initializing function HTTP routes
[22.07.2020 10:07:14] No HTTP routes mapped
[22.07.2020 10:07:14]
[22.07.2020 10:07:14] Host initialized (768ms)
[22.07.2020 10:07:16] Host started (2577ms)
[22.07.2020 10:07:16] Job host started
Hosting environment: Development
Content root path: C:\...\pythonTries\pyFuncAppEventHub1
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
[22.07.2020 10:07:21] Host lock lease acquired by instance ID '000000000000000000000000F61C9814'.
[22.07.2020 10:07:22]  INFO: Starting Azure Functions Python Worker.
[22.07.2020 10:07:22]  INFO: Worker ID: somecode, Request ID: somecode, Host Address: someaddress
[22.07.2020 10:07:22]  INFO: Successfully opened gRPC channel to someaddress
[22.07.2020 10:07:22]  INFO: Detach console logging. Switch to gRPC logging
[22.07.2020 10:07:22] Detach console logging. Switch to gRPC logging
[22.07.2020 10:07:22]  INFO: Received WorkerInitRequest, request ID somecode
[22.07.2020 10:07:22] Received WorkerInitRequest, request ID somecode
[22.07.2020 10:07:22]  INFO: Received FunctionLoadRequest, request ID: somecode, function ID: somecode
[22.07.2020 10:07:22] Received FunctionLoadRequest, request ID: somecode, function ID: somecode
[22.07.2020 10:09:32]  INFO: Successfully processed FunctionLoadRequest, request ID: somecode, function ID: somecode
[22.07.2020 10:09:35] Successfully processed FunctionLoadRequest, request ID: 6somecode, function ID: somecode

我启动将遥测消息发送到集线器的 ac# 模拟器,我可以看到在 Azure 门户中收到的消息,但这些函数没有显示任何日志。我已经尝试了 4 天来为此类问题找到任何示例或解决方案,但没有成功。我希望你能帮助我。

标签: azure-functionsvscode-python

解决方案


更新。我也检查了门户网站,我遇到了同样的问题。我意识到我的 IoT 中心有一条到存储 blob 的路由,并在本文中读到,如果存在现有路由,则集线器中的内置端点不起作用。我禁用了到存储的路由,并且能够使用该功能记录来自 IoT 中心的消息。在门户网站和 vs-code 中。我的 python 代码有一些不好的引用,但我尝试使用我的 c# 脚本(我已经工作了更多)并且我能够看到我的日志!我希望它会帮助你。我认为我应该在 EventGrid 中创建一个主题,以与存储路由并行调用我的函数。在此处输入图像描述


推荐阅读