I have a function app written in C# and deployed on Azure. In order to achieve correlation, I want to add some Custom Properties into the default RequestTelemetry of the Azure Function:
My function looks like as follow:
[FunctionName("Upload")]
[StorageAccount("AzureWebJobsStorage")]
public static async Task<IActionResult> Upload(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
string name,
ILogger log,
ExecutionContext context)
{
var requestTelemetry = req.HttpContext?.Features.Get<RequestTelemetry>();
....
return new OkObjectResult(name + "Uploaded successfully.");
}
According to my research, most people suggest using following code:
requestTelemetry.Context.GlobalProperties.Add("someproperty", "123");
or
// Deprecated
requestTelemetry.Context.Properties.Add("someproperty", "123");
But it doesn't work.
So I wonder if it is even possible to modify the Custom Properties of the default RequestTelemetry in Azure Function(V3). Does anyone have other tips about how to achieve this?
There are some ways. One is like this:
var requestTelemetry = req.HttpContext.Features.Get<RequestTelemetry>();
requestTelemetry.Properties.Add("aProp", "aValue");
The other one is
Activity.Current.AddTag("aProp", "aValue");
I have a full working demo project in a personal repo found here that demonstrates the code in this answer.