简体   繁体   中英

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

I would like to get the context id of current Azure Function execution to be included in the content of the response if there's any error during execution. My intention is to help me during troubleshooting by quickly find the traces of respective execution with its id. Here is what the code looks like:

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

And here is how the context id looks like in Azure Function monitor: 在此处输入图片说明

Is it possible to get the context id of the current Azure Function execution? If yes, how can I get it?

This should give you,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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