0

We need OperationId value inside a HTTP Trigger Azure Function. How can we get it. Highlighted OperationID in the image

Fetching OperationId.png

Saranyan
  • 13
  • 3

2 Answers2

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

enter image description here

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