简体   繁体   English

Azure 函数:运行/测试模式下出现 500 个内部服务器错误

[英]Azure Function: 500 internal internal server error in Run/Test Mode

I want to Test my azure function using the Azure Apps feature to Run/Test mode but it is throwing the '500 internal server error'.我想使用 Azure Apps 功能测试我的 azure 函数以运行/测试模式,但它抛出“500 内部服务器错误”。 I am able to debug the same code in my local environment but when to trigger the same code on the azure portal then it is getting failed without any proper error logs.我能够在我的本地环境中调试相同的代码,但是当在 azure 门户上触发相同的代码时,它会在没有任何正确的错误日志的情况下失败。 在此处输入图像描述 This Azure function will read the json format data from the event hub and write the same to the blob storage.此 Azure 函数将从事件中心读取 json 格式的数据并将其写入 blob 存储。 I am using python for the azure function development.我正在使用 python 进行 azure 函数开发。 Here is the code: init .py这是代码: init .py

from typing import List
import logging
import os
import azure.functions as func
from azure.storage.blob import BlobClient
import datetime
import json

storage_connection_string = os.getenv('storage_connection_string_FromKeyVault')

container_name = os.getenv('storage_container_name_FromKeyVault')

today = datetime.datetime.today()


def main(events: List[func.EventHubEvent]):
    for event in events:
        a = event.get_body().decode('utf-8')
        json.loads(a)
        logging.info('Python EventHub trigger processed an event: %s', a)
        logging.info(f'  SequenceNumber = {event.sequence_number}')
        logging.info(f'  Offset = {event.offset}')

        blob_client =  BlobClient.from_connection_string(storage_connection_string, container_name, str(today.year) +"/" + str(today.month) + "/" + str(today.day) + "/" + str(event.sequence_number) + ".json")

        blob_client.upload_blob(event.get_body().decode(),blob_type="AppendBlob")

local.settings.json本地设置.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<Endpoint1>",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "storage_connection_string_FromKeyVault": "<connectionString",
    "storage_container_name_FromKeyVault": "<container_name>",
    "EventHubReceiverPolicy_FromKeyVault": "<Endpoint2>"
  }
}

function.json函数.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "in",
      "eventHubName": "pwo-events",
      "connection": "EventHubReceiverPolicy_FromKeyVault",
      "cardinality": "many",
      "consumerGroup": "$Default",
      "dataType": "binary"
    }
  ]
}

Please note that this error is throwing when I am clicking on Run/Test on the portal.请注意,当我在门户网站上单击运行/测试时,会抛出此错误。 but the same code is also running fine after deployment.但相同的代码在部署后也运行良好。

The 500 error is not helpful to solve this problem, you need to check the specific error of the azure function. 500错误对解决这个问题没有帮助,需要查看azure函数的具体错误。 You can use application insights to get the details error.您可以使用应用程序洞察来获取详细信息错误。 The function must configure the corresponding application insights before you can view the log on the portal.该功能必须配置相应的应用洞察,才能在门户上查看日志。

So you need to configure an application insights to your function app like this:因此,您需要像这样为您的函数应用配置应用程序洞察:

在此处输入图片说明

Then your function app will restart.然后您的函数应用程序将重新启动。

Of course, you can also go to kudu to view:当然,你也可以去kudu查看:

First, go to advanced tools, then click 'GO',首先,转到高级工具,然后单击“开始”,

在此处输入图片说明

Then After you go to kudu, click Debug Console -> CMD -> LogFiles -> Application -> Functions -> yourtriggername.然后进入kudu后,点击Debug Console -> CMD -> LogFiles -> Application -> Functions -> yourtriggername。 You will find log file there.你会在那里找到日志文件。

If you are based on linux OS, after go to kudu, just click 'log stream'(this is not supportted to consumption plan for linux.).如果你是基于linux操作系统,进入kudu后,点击“日志流”即可(linux消费计划不支持此功能)。

I had this problem and I found that problem was with dependencies.我遇到了这个问题,我发现问题出在依赖项上。 Removing unexisting libraries (or using Microsoft's bring dependency document) will solve the issue.删除不存在的库(或使用 Microsoft 的 bring dependency document)将解决问题。

Adding third-party dependencies in the Azure portal is currently not supported for Linux Consumption Function Apps. Linux 消费功能应用目前不支持在 Azure 门户中添加第三方依赖项。 Click here to setup local environment.单击此处设置本地环境。 Learn more学到更多

If you need dependencies, to solve this problem, you can refer to this Microsoft Document如果需要依赖,解决这个问题,可以参考这个微软文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM