We need OperationId value inside a HTTP Trigger Azure Function. How can we get it. Highlighted OperationID in the image
Asked
Active
Viewed 1,471 times
2 Answers
3
You can use Activity.Current.RootId
to get Operation Id inside a HTTP Trigger in portal.
Code:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Diagnostics;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
log.LogInformation($"Activity Current RootId:{Activity.Current.RootId}");
string operationId = Activity.Current.RootId;
return new OkObjectResult("success");
}

Steve Johnson
- 8,057
- 1
- 6
- 17
-
Acivity.Current.RootId is getting the value. But, Activity.Current is returning null. Please help on this. Attached the image: Fetching OperationId.png – Saranyan Feb 25 '21 at 06:31
-
@Saranyan This i s a bug. You can directly create Function in portal and use this code. You can get it. – Steve Johnson Feb 25 '21 at 07:53
-
Hi Zhao, i tried creating Function in Portal and used the given code. But, still it is giving null reference exception.Let me know, if any other way, TQ.. – Saranyan Feb 26 '21 at 05:14
-
@Saranyan I updated my answer. Did you do like that? – Steve Johnson Feb 26 '21 at 06:29
-
Thanks Zhao, I created Function in Portal and i am getting the OperationID. All my Azure Functions are wrote using Visual Studio 2019 and not directly in Portal. How to get the Operation ID. You have any solution? Thanks for all your Replies. – Saranyan Mar 01 '21 at 07:12
-
No, I haven't found any solution to solve this. – Steve Johnson Mar 02 '21 at 07:50
0
You can add the execution context to your function.
Something like this :
[FunctionName("HttpTriggerCSharp")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req, ILogger log,ExecutionContext context)
You can access the Context.InvocationId
which will be your requirement (operationid).

Satya V
- 3,811
- 1
- 6
- 9
-
I have tested this. It isn't the same as operationid. You can have a try. – Steve Johnson Feb 25 '21 at 01:49
-
1You can have a look at this https://github.com/Azure/azure-functions-host/issues/5014. – Steve Johnson Feb 25 '21 at 02:03
-
It looks like ExecutionContext was replaced with FunctionContext as of 2023. context.InvocationId gave me the OperationId – MeanGreen Aug 22 '23 at 06:19