简体   繁体   English

HTTP 触发器不返回 Azure 函数 v2 中的堆栈跟踪

[英]HTTP trigger does not return stacktrace in Azure Functions v2

Consider a simple HTTP trigger that throws an exception.考虑一个引发异常的简单 HTTP 触发器。 When I make a call to this trigger via Postman, it return a 500 Internal Server Error, but the body is empty.当我通过 Postman 调用此触发器时,它返回 500 内部服务器错误,但正文为空。 As a developer, I want to see the stacktrace so that I can quickly debug what's going on.作为开发人员,我想查看堆栈跟踪,以便快速调试正在发生的事情。

// Azure Functions v2
[FunctionName("HttpTrigger2")]
public static async Task<HttpResponseMessage> HttpTrigger2(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req)
{
    throw new System.Exception("I want to be in the response body.");
}

This is run locally.这是在本地运行的。 I have not yet tested this remotely.我还没有远程测试过。

I believe Azure Functions v1 did show the stacktrace.我相信 Azure Functions v1 确实显示了堆栈跟踪。

I know that there are various ways of inspecting the logs, eg by hooking up the environment to AppInsights.我知道有多种检查日志的方法,例如将环境连接到 AppInsights。 What I am looking for an immediate response from the server.我正在寻找服务器的即时响应。

It should be by design that v2 function doesn't return stack trace anymore like v1 does. 应当设计使v2函数不再像v1那样返回堆栈跟踪。 And on remote Azure site, neither v1 nor v2 function returns stack trace. 在远程Azure站点上,v1和v2函数均不会返回堆栈跟踪。 The design is reasonable, stack trace is used for debug while the response body is apparently not. 设计是合理的,堆栈跟踪用于调试,而响应主体显然没有。 With stack trace returned as response, we seem to expose lengthy and sometimes private info. 随着堆栈跟踪作为响应返回,我们似乎公开了冗长的信息,有时还提供了私人信息。

If we want to get exceptions as response for convenience while debugging locally, catch the exception and return it as response. 如果在本地调试时为了方便起见,希望获取异常作为响应,请捕获异常并将其作为响应返回。

To work with HttpRequestMessage 使用HttpRequestMessage

        try
        {
            throw new System.Exception("I want to be in the response body.");
        }
        catch (Exception exception)
        {
            log.LogError(exception, exception.Message);
            return req.CreateResponse(HttpStatusCode.InternalServerError, exception);
        }

In v2, we also could use HttpRequest and the response type should be IActionResult . 在v2中,我们也可以使用HttpRequest ,响应类型应该为IActionResult

        try
        {
            throw new System.Exception("I want to be in the response body.");
        }
        catch(Exception exception)
        {
            log.LogError(exception, exception.Message);
            var res = new ObjectResult(exception)
            {
                StatusCode = StatusCodes.Status500InternalServerError
            };
            return res;
        }

You can also add the env variable CLI_DEBUG to 1 in your machine to get full exceptions in local env, debugging etc您还可以在您的机器中将环境变量 CLI_DEBUG 添加到 1 以在本地环境、调试等中获得完整的异常

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

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