azure-webjobs-sdk icon indicating copy to clipboard operation
azure-webjobs-sdk copied to clipboard

Custom Metrics don't seem to work when using DI with typed ILoggers

Open ijagberg opened this issue 5 years ago • 13 comments

Function App v2.0

I am using Dependency Injection via FunctionsStartup, and my services request an ILogger<T>, which seems to be working for trace-logging, but not for metrics.

I have tried a few different variations of my host.json file to see if it is a problem with my logging filters, but nothing so far has worked. Even when I set the default filter to "Debug", no custom metrics show up in Application Insights.

"logLevel": {
      "default": "Debug"
}

I am logging metrics using the LogMetric extension method on ILogger from the Microsoft.Extensions.Logging NuGet package. This all worked fine before switching over to using an injected ILogger<T> instead of the ILogger supplied in the Run() method.

Interestingly enough, I can see some trace-logs in Application Insights that seem to correspond with my custom metric logs. There are empty messages in the log analytics that have LogLevel Information and customDimensions "prop__Name" and "prop__Value" that are the same as my metrics. So it seems like the actual messages are coming through the filters but are not being correctly identified as being metrics.

ijagberg avatar Aug 09 '19 09:08 ijagberg

cc: @brettsam

alrod avatar Aug 14 '19 00:08 alrod

This is a bug. We currently only apply the metrics if it comes from our logger that we pass in. We check the category/event pair. When you inject a logger, that category isn't what we expect.

One workaround -- you could request a TelemetryClient in your class constructor (DI will inject it) and log your metrics directly via that.

brettsam avatar Aug 23 '19 13:08 brettsam

Thanks for the update. Looking forward to the fix but trying the workaround meanwhile.

DonFrazier-zz avatar Sep 19 '19 16:09 DonFrazier-zz

This is a bug. We currently only apply the metrics if it comes from our logger that we pass in. We check the category/event pair. When you inject a logger, that category isn't what we expect.

One workaround -- you could request a TelemetryClient in your class constructor (DI will inject it) and log your metrics directly via that.

Hi Bett,

I am trying to use the DI injected TelemetryClient from within my function and when I track an event I am not seeing it in AI anywhere. I have my local settings using the correct key (verified my ILogger is sending telemetry). When I inspect the TelemetryClient instance that gets injected the instrumentation key is empty. Any ideas?

mpaul31 avatar Dec 11 '19 18:12 mpaul31

More info (that helped me) about how to use DI for TelemetryClient https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core

bluephoton avatar Jan 16 '20 06:01 bluephoton

Hey guys, Whats the progress on this? This seems to be a small problem that could have been fixed by now, no? @brettsam

icecog avatar Mar 31 '20 09:03 icecog

@brettsam is this on a roadmap?

josiahvinson avatar Nov 05 '20 21:11 josiahvinson

Looking for this too.

ahawes-clarity avatar Dec 18 '20 13:12 ahawes-clarity

Any update on this.

j03INTHECLOUD avatar Aug 19 '21 20:08 j03INTHECLOUD

@brettsam - Any updates on this issue? Its been 3+ years 🤷

Cnordbo avatar Jan 25 '22 09:01 Cnordbo

Hello from 2022 ? any luck, having same problem....

cristian-eriomenco avatar Sep 14 '22 09:09 cristian-eriomenco