title: Filling User Id Field in Application Insights from ASP.NET Core
body: <p>I would like to be able to populate the User Id field in Application Insights with my real username data.  This is an internal application, so privacy concerns with a simple username field are moot.</p>
  
  <p>As far as I can tell, all solutions available online for this strictly work in .NET Framework, not .NET Core.</p>
  
  <p>You can find <a href="https://hajekj.net/2017/03/13/tracking-currently-signed-in-user-in-application-insights/" rel="nofollow noreferrer">this solution</a> in a few places, including some old AI documentation on GitHub.  However, when I run it, I get an error on startup indicating that dependency on the scoped object IHttpContextAccessor is not acceptable from a singleton, which of course is logical.  I don't see how this could have ever worked unless a previous version of .NET Core's DI allowed it (I'm on 2.2).</p>
  
  <p><a href="https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/136" rel="nofollow noreferrer">This issue on GitHub</a> sort of spells out the problem but it was closed after the AI team pointed out that you must use a singleton.  I tried variations of what's in the OP and the first response, and while the code ran, the User Id field on AI continued to be filled with gibberish data.</p>
  
  <p>Is there any way to make the User Id field in AI fill with something useful for server requests coming from an ASP.NET Core app?</p>
  
  <h2>EDIT</h2>
  
  <p>After seeing the answer below that my code should have worked just fine, I went back and realized the exception message hadn't specifically mentioned IHttpContextAccessor:</p>
  
  <blockquote>
    <p><strong>System.InvalidOperationException:</strong> 'Cannot consume scoped service
    'Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer'
    from singleton
    'Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]'.'</p>
  </blockquote>
  
  <p>Now, my code looks just about identical to @PeterBons' answer below, so on the face of it this exception made no sense.  <code>TelemetryConfiguration</code> doesn't event appear in my code.  But then I remembered I am using Scrutor to lazily do DI registration in Startup:</p>
  
  <pre><code>    services.Scan(scan =&gt; scan
          .FromAssembliesOf(typeof(Startup), typeof(MyDbContext))
          .AddClasses()
          .AsSelfWithInterfaces().WithScopedLifetime());
  </code></pre>
  
  <p>I assume the problem was that I need to register my ITelemetryInitializer as Singleton, and this code was inadvertently de- or re-registering it as scoped.  So I changed the last two lines to:</p>
  
  <pre><code>        .AddClasses(f =&gt; f.Where(t =&gt; t != typeof(RealUserAIProvider)))
          .AsSelfWithInterfaces().WithScopedLifetime());
  </code></pre>
  
  <p>And it worked.  Rather than editing out my mistake above, I'll leave it.  @PeterBons' answer below is still going to be helpful to other people, and maybe my confusion with Scrutor will help someone too.</p>