Azure Functions 应用程序见解中的自定义属性

2024-02-28

我正在 Azure Application Insights 中监视大量应用程序。 在所有这些中,我向事件、跟踪等添加了一些自定义属性,以便我可以在门户中进行过滤/分组。

是否可以将相同的自定义属性添加到与 Azure Functions 的内置应用程序洞察集成中?

已阅读文档但找不到任何相关内容。

Edit:

我维护着托管在各种环境中的大量应用程序。其中大约 15 个是 Azure Functions。 我通过日志处理程序从所有应用程序将遥测数据发送到同一应用程序洞察实例。为了过滤/分组信息,我通过日志处理程序自动将“CustomerName”和“CustomerInstance”属性添加到所有事件。

当我从 Azure 函数获取标准事件时,很难以有用的方式呈现信息并将其与其他事件关联起来。 通过对功能应用程序进行一些巧妙的命名,我可以在分析中解析请求 URL,但不能在门户中解析。


您可以使用显式添加这些自定义属性telemetry.Context.Properties.Add() method.

我用函数 v2 做了一个演示,如下所示:

1.在Visual Studio中创建函数v2

2.然后在视觉工作室中添加Microsoft.ApplicationInsights2.8.1(最新版本)通过nuget包管理器

3.在您的Function.cs中,编写以下代码:

using Microsoft.ApplicationInsights;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;

namespace FunctionApp17
{
    public static class Function1
    {
        private static string key = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY",EnvironmentVariableTarget.Process);
        private static TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey= key };

        [FunctionName("Function1")]
        public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log)
        {
            if (!telemetry.Context.Properties.ContainsKey("Function_appName"))
            {
                telemetry.Context.Properties.Add("Function_appName", "myfuncapp111");
            }
            else
            {
                telemetry.Context.Properties["Function_appName"] = "myfuncapp111";
            }
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            telemetry.TrackEvent("event111");
            telemetry.TrackTrace("trace111");
        }
    }
}

4.Publish to azure, and in your function app -> Application settings, add the instrumentation key: enter image description here

5.函数应用程序运行后,导航到您的应用程序见解 -> 搜索,您可以添加在代码中定义的过滤器。

Then you can see the filtered message: enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Azure Functions 应用程序见解中的自定义属性 的相关文章

随机推荐