Azure / azure-webjobs-sdk

Azure WebJobs SDK
MIT License
737 stars 357 forks source link

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

Open ijagberg opened 5 years ago

ijagberg commented 5 years ago

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 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 "propName" and "propValue" 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.

alrod commented 5 years ago

cc: @brettsam

brettsam commented 5 years ago

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.

DonFrazier-zz commented 4 years ago

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

mpaul31 commented 4 years ago

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?

bluephoton commented 4 years ago

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

icecog commented 4 years ago

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

josiahvinson commented 3 years ago

@brettsam is this on a roadmap?

ahawes-clarity commented 3 years ago

Looking for this too.

j03INTHECLOUD commented 3 years ago

Any update on this.

Cnordbo commented 2 years ago

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

cristian-eriomenco commented 1 year ago

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

gluff95 commented 1 month ago

This is almost five years old - still no progress on fixing this?

karun-verghese commented 1 week ago

@mpaul31 Not sure if you're still facing the same issue.

I recently came across this thread and I have a webjob using the .Net Core webjobs SDK and using the package Microsoft.Azure.WebJobs.Logging.ApplicationInsights

I also faced a similar issue where the TelemetryClient was injected but somehow nothing seemed to show up in app insights.

I found the steps in this article helped me get around it even though the article was written for a slightly different context. https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger?tabs=dotnet6#im-using-the-standalone-package-microsoftextensionsloggingapplicationinsights-and-i-want-to-log-more-custom-telemetry-manually-how-should-i-do-that