如何获取当前 Azure 函数执行的上下文 ID?

How can I get the context id of current Azure Function execution?

如果执行过程中出现任何错误,我希望将当前 Azure Function 执行的上下文 ID 包含在响应的内容中。我的目的是在故障排除期间通过使用其 ID 快速找到相应执行的痕迹来帮助我。代码如下所示:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    try
    {
        // Some code...
    }
    catch (Exception ex)
    {
        return new HttpResponseMessage(HttpStatusCode.InternalServerError)
        {
            Content = new StringContent("Insert Azure Function Context Id here...");
        };
    }
}

下面是上下文 ID 在 Azure 函数监视器中的样子:

能否获取当前Azure Function执行的context id?如果有,如何获取?

这应该给你,

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
{
    return req.CreateResponse(System.Net.HttpStatusCode.OK, context.InvocationId);
}