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

时间:2017-10-31 10:53:16

标签: c# azure azure-functions

如果执行期间出现任何错误,我希望将当前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...");
        };
    }
}

以下是Azure功能监视器中上下文ID的外观: enter image description here

是否可以获取当前Azure Function执行的上下文ID?如果是,我怎么能得到它?

1 个答案:

答案 0 :(得分:1)

这应该给你,

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