title: AddApplicationInsightsTelemetry not registering an ILoggingProvider
body: <p>I have an application where I am trying to log messages to Application Insights. I am relatively new to Application Insights and the Microsoft logging framework so I may be getting some of the terminology wrong here but I'm happy to clarify if anything is unclear.</p>
  <p>I have tried following the guide here:
  <a href="https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#aspnet-core-application" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#aspnet-core-application</a>
  i.e. simply calling the AddApplicationInsightsTelemetry method on the IServiceCollection in my Startup class and then injecting an ILogger in one of my controllers to try to log a message.</p>
  <p>I have not tried configuring a filter to set a minimum severity level so I am simply doing _logger.LogWarning(&quot;My message&quot;) since warnings should be logged by default.</p>
  <p>After hitting my controller I checked the traces in Application Insights but could not see my message there. I did however see telemetry data, e.g. I could see requests hitting my application being logged.</p>
  <p>I then attached a debugger and stepped through the code in my controller and noticed that my ILogger did not contain an ApplicationInsightsLogger. I also tried to inject an ILoggerFactory and calling the CreateLogger method on it with the same result, i.e. the logger does not contain an ApplicationInsightsLogger.</p>
  <p>This seems to indicate that for some reason an ApplicationInsightsLoggerProvider is not being registered by the call to AddApplicationInsightsTelemetry or that something is clearing the LoggerProviders after it has been added but I can not see anything in my Startup class that seems like it would do that.</p>
  <p>I should mention that there is another project in the same solution, using the same version of Microsoft.ApplicationInsights.AspNetCore (2.14.0) where I do the same thing but there the log messages do show up in Application Insights.</p>
  <p>Any idea at all what the issue here could be or how I could continue troubleshooting it?</p>
  <p><strong>Update</strong></p>
  <p>I've done some more digging into this and the problem definitely be that for some reason the call to AddApplicationInsightsTelemetry(&quot;myInstrumentationKey&quot;) is not registering an ILoggerProvider. I experimented with doing the following in Startup.ConfigureServices:</p>
  <pre><code>services.AddApplicationInsightsTelemetry(&quot;myInstrumentationKey&quot;);
  var sp = services.BuildServiceProvider();
  var loggerFactory = sp.GetService&lt;ILoggerFactory&gt;();
  </code></pre>
  <p>And when I attached the debugger and inspected the resolved loggerFactory the _providerRegistrations property is empty. In the other project in the same solution where I have gotten this to work this property contains an Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider. This seems to me to rule out the option that something later on in Startup is clearing the registered providers, it's simply not getting added in the first place.</p>
  <p>Is there any reason that anyone knows why this call should fail to register an ApplicationInsightsLoggerProvider? Is there anyway I could find out why it fails to do so? I've tried checking the Windows Event log but can't see any errors or warnings related to this.</p>
  <p><strong>Update 2</strong></p>
  <p>I forgot about this question but here's another late update.</p>
  <p>The project where I <em><strong>do not</strong></em> get an ApplicationInsightsLoggerProvider uses AspNetCore 1.1.1 and the project where I <em><strong>do</strong></em> get an ApplicationInsightsLoggerProvider is using AspNetCore 2.0.2. So my assumption for is that this is at least in part what is causing the issue. Both of them have net461 as their target framework.</p>
  <p>In the end I managed to get around this issue by calling</p>
  <pre><code>loggerFactory.AddApplicationInsights(app.ApplicationServices);
  </code></pre>
  <p>in the Configure method in my Startup class.</p>
  <p>I get an obsolete warning stating that I should be using the AddApplicationInsightsTelemetry extension method on IServiceCollection (which, as stated, doesn't work) or the UseApplicationInsights extension method on IWebHostBuilder which is also marked as obsolete in favor of AddApplicationInsightsTelemetry.
  It also claims that using the AddApplicationInsights method will result in double logging but that does not seem to happen in my case either so until we get around to updating this project to use a newer version of AspNetCore I think this will have to do.</p>