title: Using Application Insights with ILoggerFactory
body: <p>I'm trying to log exceptions to Application Insights. I succeeded in doing this by calling <code>TelemetryClient.TrackException</code> directly. However, I would like to abstract away from this in my code in case I'd want to log to other platforms in the future, so I would like to stick to only the ILogger interface.</p>
  
  <p>I found that you can use <code>ILoggerFactory.AddApplicationInsights</code> (as implemented <a href="https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/edbdca20b3c87358825e17096251fcb64de5d47b/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation/ApplicationInsightsLoggerFactoryExtensions.cs" rel="noreferrer">here</a>) but no matter what I did, I don't see the logs showing up in ApplicationInsights with this. </p>
  
  <p>Below is my code:</p>
  
  <p><code>Startup.cs</code></p>
  
  <pre><code>    IConfigurationRoot Configuration { get; set; }
      ILoggerFactory LoggerFactory { get; set; }
      IServiceProvider ServiceProvider { get; set; }
  
      public Startup( IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory )
      {
          this.LoggerFactory = loggerFactory;
          string configurationFilePath = "abc.json";
  
          this.Configuration = new ConfigurationBuilder()
              .SetBasePath( hostingEnvironment.ContentRootPath )
              .AddJsonFile( configurationFilePath, optional: true, reloadOnChange: true )
              .AddEnvironmentVariables()
              .Build();
      }
  
      public void Configure(
          IApplicationBuilder applicationBuilder,
          IHostingEnvironment hostingEnvironment,
          ILoggerFactory loggerFactory,
          IServiceProvider serviceProvider )
      {
          this.ServiceProvider = serviceProvider;
          loggerFactory.AddApplicationInsights( serviceProvider );
          applicationBuilder.UseMvc();
      }
  
      public void ConfigureServices( IServiceCollection services )
      {
          services.AddApplicationInsightsTelemetry( this.Configuration );
          services.AddMvc( .... // A bunch of options here ... )
      }
  </code></pre>
  
  <p>Then, I try to use this in my controller like this:</p>
  
  <pre><code>    ILogger&lt;XController&gt; Logger { get; set; }
  
      public XController( ILogger&lt;XController&gt; logger )
      {
          this.Logger = logger;
      }
  
      [HttpPost]
      [Route( "v3.0/abcd" )]
      public async Task PostEvent( [FromBody] XEvent xEvent )
      {
          this.Logger.LogError( 0, new Exception( "1234" ), "1234" );
      }
  </code></pre>
  
  <p>However, I don't see any exceptions associated with the request at all. If I replace the <code>Logger.LogError</code> line with <code>TelemetryClient.TrackException</code> (and create the <code>TelemetryClient</code> first), then I can see the exception without any issues. </p>
  
  <p>I don't know what I'm doing wrong. Could anyone help? </p>