gpt4 book ai didi

Azure Function 详细记录内部服务器错误

转载 作者:行者123 更新时间:2023-12-02 06:29:16 24 4
gpt4 key购买 nike

我有一个带有 HTTP 触发器的 v2 Azure Function。有时,它会返回 500,不带任何其他信息。我想将其设置为记录更详细的信息,包括错误消息和堆栈跟踪。

我尝试将我的 host.json 设置为如下所示:

{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Trace"
}
}
}

但在本地运行时并没有产生任何更详细的信息。

内置的日志设置似乎让我失败,我正在尝试顶级 try/catch,但我不知道如何返回两者状态代码 500 和异常对象。

[FunctionName("Http-UploadFiles")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "uploadFiles")] HttpRequest request,
ILogger log)
{
try
{
var requestBody = await new StreamReader(request.Body).ReadToEndAsync();
var body = JsonConvert.DeserializeObject<JObject>(requestBody);

var inventorySetGuid = Guid.Parse(body["inventorySetGuid"].ToString());
var files = body["files"].Select(FileComponentUtilities.GetFile).ToList();
var componentsAndErrors = await FileComponentUtilities.InsertSuitableComponentFiles(files, inventorySetGuid);

return new JsonResult(componentsAndErrors, StorageFramework.Storage.SerializerSettings);
}
catch (Exception exception)
{
return new StatusCodeResult(500);
}
}

此代码返回状态码500,但不返回异常对象。我找不到合适的 IActionResult 来返回带有异常对象的 500。如何为 v2 Azure Function 实现此目的?

最佳答案

您可以返回一个 ContentResult,它允许您设置任何状态代码、内容和内容类型。

return new ContentResult
{
StatusCode = 500,
Content = "Something went wrong"
};

但是不要使用它来返回堆栈跟踪!这是您的应用程序的安全问题,有更好的方法...打开 Application Insights在您的函数应用程序中并使用它来查看您的跟踪和错误。

关于Azure Function 详细记录内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com