title: System.Diagnostics.Activity is null in aspnet core 2.1
body: <p>We've just updated an aspnet core 2.0 application to 2.1 and have run into a problem with our usage/reliance on <code>System.Diagnostics.Activity</code>.</p>
  
  <p><strong>Background</strong></p>
  
  <p>We want a consistent 'correlation id' passed across our service boundaries, so that we can correlate log entries per request.</p>
  
  <p>The approach we took was:</p>
  
  <ul>
  <li><p>Add a diagnostic listener to the middleware pipeline for <code>Microsoft.AspNetCore.Hosting.HttpRequestIn.Start</code></p></li>
  <li><p>When this listener was invoked, check if there was a header in the context.request called "Request-Id", and if there wasn't then add one with a value of <code>Activity.Current.Id</code></p></li>
  <li><p>Another piece of middleware took care of pushing the "Request-Id" header value into the logging context</p></li>
  </ul>
  
  <p>This worked just fine in 2.0 and the hierarchical nature of <code>Activity.Current.Id</code> meant we could correlate log entries across different services and layers. In 2.1 we are now getting exceptions because <code>Activity.Current</code> appears to always be null on the point-of-entry for a request (i.e. the first service that is hit, in this case an API). </p>
  
  <p>I've not managed to find any information that suggests that an activity is no longer automatically started whenever an HttpRequest comes in, but that's what it seems like is happening. Is anyone able to shed any light on what has changed and/or what we're doing wrong?</p>
  
  <p><strong>Some code</strong></p>
  
  <p>The startup configure method ...</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, DiagnosticListener diagnosticListener)
          {
             diagnosticListener.SubscribeWithAdapter(new HttpRequestInDiagnosticListener());
             app.UseMiddleware&lt;SetRequestIdHeaderForHttpRequestInMiddleware&gt;();
  
              if (env.IsDevelopment())
                  app.UseDeveloperExceptionPage();
              else
                  app.UseStatusCodePages();
  
              app.UseSecurityHeaders(Headers.AddSecurityHeaders());
  
              app.UseMvc();
          }
  </code></pre>
  
  <p>... and the custom classes involved</p>
  
  <pre><code>    public class HttpRequestInDiagnosticListener
      {
          [DiagnosticName("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")]
          public virtual void OnMiddlewareStarting(HttpContext httpContext)
          {
              Console.WriteLine($"Middleware Starting, path: {httpContext.Request.Path}");
          }
      }
  
      public class SetRequestIdHeaderForHttpRequestInMiddleware
      {
          private readonly RequestDelegate _next;
          private readonly DiagnosticSource _diagnostics;
  
          public SetRequestIdHeaderForHttpRequestInMiddleware(RequestDelegate next, DiagnosticSource diagnosticSource)
          {
              _next = next;
              _diagnostics = diagnosticSource;
          }
  
          public async Task Invoke(HttpContext context)
          {
  
              if (_diagnostics.IsEnabled("Microsoft.AspNetCore.Hosting.HttpRequestIn.Start"))
              {
                  if (!context.Request.Headers.Keys.Contains("Request-Id"))
                  {
                      context.Request.Headers.Add("Request-Id", Activity.Current.Id);
                  }
              }
  
              await _next.Invoke(context);
          }
      }
  </code></pre>