Custom properties in azure function application insights

风流意气都作罢 提交于 2021-01-03 06:55:28

问题


I am monitoring a lot of applications in Azure Application Insights. In all of them I add some custom properties to events, traces etc so I can filter/group in the portal.

Is it possible to add the same custom properties to the built in application insight integration with Azure Functions?

Have read the documentation but can't find anything about it.

Edit:

I maintain a large number of applications hosted in various environments. About 15 of these are Azure Functions. From all my applications I send telemetry to the same application insights instance via a log handler. To filter/group the information I add "CustomerName" and "CustomerInstance" properties to all events automatically via my log handler.

When I get the standard events from an Azure Function it is difficult to present information in a useful way and correlate it with other events. With some clever naming of the function apps I can parse the request url in analytics but not in the portal.


回答1:


You can add these custom properties explicitly using telemetry.Context.Properties.Add() method.

I did a demo with function v2 as below:

1.Create a function v2 in visual studio

2.Then in the visual studio, add Microsoft.ApplicationInsights 2.8.1(latest version) via nuget package manager

3.In your Function.cs, write the following code:

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:

5.After the function app is running, nav to your application insights -> search, you can add your filters which defined in your code.

Then you can see the filtered message:



来源:https://stackoverflow.com/questions/53322789/custom-properties-in-azure-function-application-insights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!