ApplicationInsights-dotnet: Unable to inject ILogger in telemetry processor

Based on an existing .NET Core web app:

Repro Steps

  1. Implement a minimal class that implements ITelemetryProcessor (e.g. CustomTelemetryProcessor);
  2. Inject ILogger<CustomTelemetryProcessor> in the constructor;
  3. Register your custom processor for dependency injection by calling:
// Telemetry
services.AddApplicationInsightsTelemetryProcessor<CustomTelemetryProcessor>();
  1. Deploy your application in an Azure App Service;
  2. After a while, try to access a well-known route, a 502 error is shown;
  3. Open the Kudu portal, then pick your favorite shell;
  4. Find your deployed app (e.g. /site/wwwroot/), then try to execute it with dotnet YourAssembly.dll;
  5. Red logs appear indicating a StackOverflowException.

Actual Behavior

It seems that the application will not be able to start, because there might be a circular reference between the logger and the custom telemetry processor, as explained on this StackOverflow thread.

Expected Behavior

If I want to debug my telemetry processor, I should be able to log something to help our investigations.

Version Info

  • SDK Version : Using Microsoft.ApplicationInsights.AspNetCore at 2.5.1 and Microsoft.ApplicationInsights at 2.8.1;
  • .NET Version : Targetting netcoreapp2.2 in the Web project;
  • How Application was onboarded with SDK (VisualStudio/StatusMonitor/Azure Extension) :
  • OS : ?
  • Hosting Info (IIS/Azure WebApps/ etc): the application is hosted on an Azure App Service that hosts the .NET Core 2.2 app.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 10
  • Comments: 18 (5 by maintainers)

Most upvoted comments

We have this issue as well in .net 5.0, although it’s manifesting itself as a DI circular dependency involving TelemetryConfiguration.

We’re trying to implement an ITelemetryInitializer which attaches some user metadata to app insights messages. This goes to a user cache that depends on IMemoryCache as implemented by Microsoft.Extensions.Caching.Memory.MemoryCache. MemoryCache is then dependent on ILoggerFactory, and LoggerFactory eventually depends on TelemetryConfiguration.

So we have in essence TelemetryConfiguration -> MyUserTelemetryInitializer -> MyUserCache -> MemoryCache -> LoggerFactory -> TelemetryConfiguration.

This is not being addressed in the next 2 releases. (i.e no fix at least until end of 2020). Only recommendation is to use the workaround of removing ilogger from telemetryprocessors.

This is still an issue, please remove the stale label

I have the same issue with my ITelemetryInitializer. I wanted to use a service which brings more context to the initializer which has a Logger that is injected. Result: Circular Dependency Detected. I need the logger in that service so what would be the solution? I’m using .net core 3.1 and Microsoft.ApplicationInsights.AspNetCore 2.15.0

Sooo… when, if ever, is this going to be addressed? Should be highest priority IMO since it’s breaking people upgrading from Core 2.x.

Thanks @cijothomas makes sense. I’ll keep an eye on this issue and do what I can to mitigate the issue with our processors. Your quick reply was much appreciated 😃

https://github.com/microsoft/ApplicationInsights-dotnet/issues/1536#issuecomment-573903440 This explains the reason. Since application insights logger provider is added, there is circular dependency now. TelemetryProcessors require Ilogger injected. Ilogger requires TelemetryConfig to be created, which requires TelemetryProcessors.

This issue hasn’t been solved, and at this point, only workaround seems like removing Ilogger from the TelemetryProcessors constructor. If you cannot remove this, then the only other workaround (non trivial) is not NOT register applicationinsights using services.AddApplicationInsights(). Instead manually construct every module and telemetryconfguration.

I am unsure if this is the same issue or a separate one, but we noticed the same problem with IDataProtectionProvider instead of ILogger, but it only started happening after upgrading from .NET Core 2.2 to .NET Core 3.1. (We use IDataProtectionProvider to unprotect a cookie value, and were setting that to the requestTelemetry’s AccountId property.) The app will not start up, and if you really debug deep into it (which is not easy) you find a stack overflow like described above.

I hope you can easily reproduce this by simply adding IDataProtectionProvider to the constructor of the custom telemetry initializer class.

Please let me know if I should file a separate GitHub issue for this or if it is the same problem.