![](/img/trans.png)
[英]Azure Function App : Error when loading V3 in portal for fresh function app?
[英]Add Custom Properties for RequestTelemetry of the Azure Function (v3)
我有一個用 C# 編寫並部署在 Azure 上的 function 應用程序。為了實現關聯,我想將一些自定義屬性添加到 Azure Function 的默認 RequestTelemetry 中:
我的 function 如下所示:
[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.");
}
根據我的研究,大多數人建議使用以下代碼:
requestTelemetry.Context.GlobalProperties.Add("someproperty", "123");
或者
// Deprecated
requestTelemetry.Context.Properties.Add("someproperty", "123");
但它不起作用。
所以我想知道是否有可能在 Azure 函數 (V3) 中修改默認 RequestTelemetry 的自定義屬性。 有沒有人有關於如何實現這一目標的其他技巧?
有一些方法。 一個是這樣的:
var requestTelemetry = req.HttpContext.Features.Get<RequestTelemetry>();
requestTelemetry.Properties.Add("aProp", "aValue");
另一個是
Activity.Current.AddTag("aProp", "aValue");
我在此處找到的個人回購中有一個完整的工作演示項目,演示了此答案中的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.