代码之家  ›  专栏  ›  技术社区  ›  Thomas Schreiter

HTTP触发器在Azure函数v2中不返回stacktrace

  •  -1
  • Thomas Schreiter  · 技术社区  · 6 年前

    考虑抛出异常的简单HTTP触发器。当我通过Postman调用这个触发器时,它返回一个500内部服务器错误,但正文是空的。作为一名开发人员,我希望看到stacktrace,以便能够快速调试正在发生的事情。

    // 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.");
    }
    

    这是在本地运行的。我还没有远程测试过这个。

    我相信Azure Functions v1确实显示了stacktrace。

    我知道检查日志有多种方法,例如通过将环境连接到AppInsights。我所寻找的是服务器的即时响应。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Jerry Liu Phantom    6 年前

    根据设计,v2函数不再像v1那样返回堆栈跟踪。在远程Azure站点上,v1和v2函数都不会返回堆栈跟踪。设计合理,使用堆栈跟踪进行调试,而响应体显然不是。当堆栈跟踪作为响应返回时,我们似乎暴露了冗长的、有时是私有的信息。

    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);
            }
    

    在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;
            }