Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ILogger<T>.LogMetric of Azure function is not logging messages into Application Insight

I am using Azure function v3 on .net core 3.x where ILogger<T> is created through dependency injection of respective class T through IServiceProvider like ILogger<T> _log = _log = serviceProvider.GetService<ILogger<T>>();

Other than _log.LogMetric() every other log messages(traces, information, error, exception etc) are getting pushed to Application Insight. I have checked traces section of transaction search as well as traces and custom metrics of logs.

Even host.json file logging configuration seems to be correct. So what I am doing wrong?

like image 624
Riktim Mondal Avatar asked Nov 23 '25 20:11

Riktim Mondal


1 Answers

There is a post about this on Github that suggests this is a bug. There is a workaround to inject the TelemetryClient and use client.TrackMetric();

You can do this: services.AddSingleton<TelemetryClient>(x => new TelemetryClient(configuration.GetSection("ApplicationInsights:InstrumentationKey").Get<string>()))

The Configuration here, being your IConfiguration or IConfigurationRoot

Then after injecting into your service, just call the method, client.TrackMetric(metricName, metricValue, dictionaryProperties);

Post in question: https://github.com/Azure/azure-webjobs-sdk/issues/2308

Just a note about this, is that if you're using the TelemetryClient, you'll want to aggregate these metrics yourself with this workaround:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.telemetryclient.trackmetric?view=azure-dotnet

like image 173
Lawrence Eby Avatar answered Nov 25 '25 16:11

Lawrence Eby