'appication insights operation_id being changed

With applicaiton insights logging, I am trying to get the operation_id to be a guid that we use throuhgout the rest of the system. I pull a row out of the database, which already has a correlationID, and I want to use this correlationID as the operation_id when I log to applicaiton insights.

I have the StartOperation method below:

internal static IOperationHolder<RequestTelemetry> StartOperation(TelemetryConfiguration configuraiton, string operationName, string correlationId)
{
    TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
    config.ConnectionString = configuraiton.ConnectionString;
    CorrelatingTelemetryInitializer initializer = new CorrelatingTelemetryInitializer(correlationId);
    config.TelemetryInitializers.Add(initializer);
    TelemetryClient tc = new TelemetryClient(config);
    var operation = tc.StartOperation<RequestTelemetry>(operationName);
    return operation;
}

This creates a new configuraiton, so I can add a custom initializer, and uses the exising connect string. It added my custom initializer that takes the correlationID as a parameter. Then I create a new TelemetryClient and start an operation from that.

I then use it like this:

using (var operation =  EventTelemetry.StartOperation(_telemetryConfiguration, "Publishing Event", domainEvent.CorrelationId.ToString()))
{
_logger.LogWarning($@"Testing with operaiton ID {operation.Telemetry.Context.Operation.Id}");
}

In my log message I am logging the operation id and I see that it is set to the guid. However, the operation_id shown for applicaiton insights is different.

enter image description here



Solution 1:[1]

  • set both RequestTelemetry.Id and Context.Operation.Id with the internal Id your application uses.
var client =  new  TelemetryClient();  
var request =  new  RequestTelemetry(/* request parameters */);
request.Id  = internalId; 
client.Context.Operation.Id  = internalId; 
// all telemetry tracked through this client will now be linked to the request client.Track(request);
  • For further details check this link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SaiSakethGuduru-MT