天蓝色功能应用程序见解中的自定义属性

时间:2018-11-15 15:32:46

标签: azure-functions azure-application-insights

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

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

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

编辑:

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

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

1 个答案:

答案 0 :(得分:2)

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

我使用功能v2进行了演示,如下所示:

1。在Visual Studio中创建功能v2

2。然后在Visual Studio中,通过nuget软件包管理器添加Microsoft.ApplicationInsights 2.8.1(最新版本)

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。发布到天蓝色,然后在功能应用程序->应用程序设置中,添加检测键: enter image description here

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

然后您将看到过滤的消息: enter image description here

相关问题