title: ApplicationInsights dotnet core custom initializer
body: <p>I'm unable to figure out how to configure AI for aspnetcore project. I've done the following:</p>
  
  <pre><code>services.AddSingleton&lt;ITelemetryInitializer, AppInsightsInitializer&gt;();
  services.AddApplicationInsightsTelemetry(Configuration);
  </code></pre>
  
  <p>Where I need the loggedin user and the servicename so I've got this initializer: </p>
  
  <pre><code>public class AppInsightsInitializer : ITelemetryInitializer
  {
      private IHttpContextAccessor _httpContextAccessor;
      public AppInsightsInitializer(IHttpContextAccessor httpContextAccessor)
      {
          _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException("httpContextAccessor");
      }
      public void Initialize(ITelemetry telemetry)
      {
          var httpContext = _httpContextAccessor.HttpContext;
          telemetry.Context.Properties["appname"] = "MyCoolService";
  
          if (httpContext != null &amp;&amp; httpContext.User.Identity.IsAuthenticated == true &amp;&amp; httpContext.User.Identity.Name != null)
          {
              telemetry.Context.User.AuthenticatedUserId = httpContext.User.Identity.Name;
          }
      }
  
  }
  </code></pre>
  
  <p>I've got no applicationinights.config file (I understand they are not needed)</p>
  
  <p><strong>Problem</strong>: I got 4 entries of each log (same id). The data is correct. I also got the following errror in the logs: </p>
  
  <pre><code>AI: Error collecting 9 of the configured performance counters. Please check the configuration.  Counter \Process(??APP_WIN32_PROC??)\% Processor Time: Failed to perform the first read for performance counter. Please make sure it exists. 
  Category: Process, counter: % Processor Time, instance Counter \Memory\Available Bytes: Failed to register performance counter. 
  Category: Memory, counter: Available Bytes, instance: . 
  Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Requests/Sec: Failed to perform the first read for performance counter. Please make sure it exists. 
  Category: ASP.NET Applications, counter: Requests/Sec, instance MyCoolv3.Api.exe Counter \.NET CLR Exceptions(??APP_CLR_PROC??)\# of Exceps Thrown / sec: Failed to perform the first read for performance counter. Please make sure it exists. 
  Category: .NET CLR Exceptions, counter: # of Exceps Thrown / sec, instance Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time: Failed to perform the first read for performance counter. Please make sure it exists. 
  Category: ASP.NET Applications, counter: Request Execution Time, instance MyCoolv3.Api.exe Counter \Process(??APP_WIN32_PROC??)\Private Bytes: Failed to perform the first read for performance counter. Please make sure it exists. Category: Process, counter: Private Bytes, instance Counter \Process(??APP_WIN32_PROC??)\IO Data Bytes/sec: Failed to perform the first read for performance counter. Please make sure it exists. 
  Category: Process, counter: IO Data Bytes/sec, instance Counter \ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue: Failed to perform the first read for performance counter. Please make sure it exists. Category: ASP.NET Applications, counter: Requests In Application Queue, instance MyCoolv3.Api.exe 
  Counter \Processor(_Total)\% Processor Time: Failed to register performance counter. Category: Processor, counter: % Processor Time, instance: _Total.
  </code></pre>
  
  <p>I'm using:</p>
  
  <pre><code>&lt;PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.0-beta1" /&gt;
  &lt;PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /&gt;
  &lt;PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" /&gt;
  </code></pre>