c# - 为 Azure Function (v3) 的 RequestTelemetry 添加自定义属性

标签 c# azure azure-functions azure-application-insights

我有一个用 C# 编写并部署在 Azure 上的函数应用程序。为了实现关联,我想在Azure Function的默认RequestTelemetry中添加一些自定义属性:

enter image description here

我的函数如下所示:

    [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 Function(V3)中默认RequestTelemetry的自定义属性。有没有人有关于如何实现这一目标的其他提示?

最佳答案

有一些方法。一种是这样的:

var requestTelemetry = req.HttpContext.Features.Get<RequestTelemetry>();
requestTelemetry.Properties.Add("aProp", "aValue");

另一个是

Activity.Current.AddTag("aProp", "aValue");

我在个人存储库中有一个完整的工作演示项目 here演示了这个答案中的代码。

关于c# - 为 Azure Function (v3) 的 RequestTelemetry 添加自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68848971/

相关文章:

python - 天蓝色函数 : How do I write my logs to Azure Application Insights using local debug mode with Azure Functions using Python?

具有多个输出到同一 EventHub 的 Azure 函数不起作用

c# - 表达式树 C# - 空条件运算符 (?.)

c# - Linq-to-xml 按没有子元素的元素分组

c# - SQLite 不在准备好的语句中保留我的前导零

azure - 使用 AzureDevOps 更新 Azure 上托管的 Sitecore 应用程序

azure - 在 Azure 权限的 IAM 中找不到集合管理员角色

c# - 标签式浏览器 CefSharp

c# - 依赖注入(inject)无法解析在 Azure 中运行的 .NET Core Function App 中的类型服务

python - 从用于 python 的 Azure 函数获取应用程序设置值(环境)