title: How to use a custom OperationId without ending up with memory leaks and it being ignored by Application Insights?
body: <p>My question is:</p>
  
  <p>What is the correct/better way to inject a custom Correlation/Operation Id in my scenario, which doesn't result in memory leaks and is correctly used and displayed on the Azure Portal?</p>
  
  <p>And here are the specific details of my scenario:</p>
  
  <p>I have a Azure Worker Role that is constantly dequeueing messages from an Azure queue and doing processing. The message contains a Correlation Id (Operation Id) and Operation Name, which I want to be used for all telemetry collected and pushed through Application Insights during the processing work for that message.</p>
  
  <p>The way this is currently implemented is through a custom <code>ITelemetryInitializer</code>, which looks something like this:</p>
  
  <pre><code>public class MiTelemetryInitialiser : ITelemetryInitializer
  {
      private readonly ILoggingContext _context;
  
      public MiTelemetryInitialiser(ILoggingContext context)
      {
          _context = context;
      }
  
      public void Initialize(ITelemetry telemetry)
      {
          if (string.IsNullOrEmpty(telemetry.Context.Operation.Id) &amp;&amp; _context != null)
          {
              telemetry.Context.Operation.Id = _context.OperationId;
  
              if (!String.IsNullOrWhiteSpace(_context.OperationName))
                  telemetry.Context.Operation.Name = _context.OperationName;
          }
      }
  }
  </code></pre>
  
  <p>Then my worker role processing loop look something like this (si:</p>
  
  <pre><code>while(!cancellationToken.IsCancellationRequested) {
      // 1. Get message from the queue
      // 2. Extract operation name and Correlation Id from the message
      TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
  
      var loggingContext = //new logging context with operation name and correlation id
      config.TelemetryInitializers.Add(new MiTelemetryInitialiser(loggingContext));
      TelemetryClient telemetryClient = new TelemetryClient(config);        
  
      // do work
  
      client.Flush();
  }
  </code></pre>
  
  <p>I am using this <code>TelemetryChannel</code>:</p>
  
  <pre><code>&lt;TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/&gt;
  </code></pre>
  
  <p>There are two issues:</p>
  
  <ol>
  <li>Memory leak (significant) - I am guessing due to the way the <code>ServerTelemetryChannel</code> is implemented (unfortunately unlike most of the .NET SDK - this channel is not open source) and due to the tight loop?</li>
  <li>Even though <code>client.TrackRequest()</code> is pushing my custom Correlation Id (verified using Fiddler) - Application Insights is overwriting it on the Azure Portal and is displaying its own Operation Id rather than mine. Other telemetry during the request has the correct OperationId which I have set.</li>
  </ol>
  
  <p>Here is a photo of the memory leak after running the loop for ~10 minutes (grows to 1GB+ after a while):</p>
  
  <p><a href="https://i.stack.imgur.com/xOduB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xOduB.png" alt="enter image description here"></a></p>