I followed the instructions hereApp Insights to write custom telemetry
But I see that the trace does not work as the document said that they share the same operation ID
Along with setting an operation context, StartOperation creates a
telemetry item of the type that you specify. It sends the telemetry
item when you dispose of the operation or if you explicitly call
StopOperation. If you use RequestTelemetry as the telemetry type, its
duration is set to the timed interval between start and stop.Telemetry items reported within a scope of operation become children
of such an operation. Operation contexts could be nested.
My test project is .net4.7.2 and installed the app insights nugget
How do I correlate the child trace with the parent request correctly? Thanks
// Establish an operation context and associated telemetry item:
using (var operation = telemetryClient.StartOperation<RequestTelemetry>("operationName"))
{
// Telemetry sent in here will use the same operation ID.
telemetryClient.TrackTrace("test"); // or other Track* calls
// Set properties of containing telemetry item--for example:
operation.Telemetry.ResponseCode = "200";
// Optional: explicitly send telemetry item:
telemetryClient.StopOperation(operation);
operation.Dispose();
telemetryClient.Flush();
} // When operation is disposed, telemetry item is sent.
How do I correlate the child trace with parent request correctly?
To corelate between logs use below code where you can have same operation id for all subsequent actions:
using (var riop = ri_tc.StartOperation<RequestTelemetry>("HomeController.Index"))
{
try
{
var rithtc = new TraceTelemetry("Hello Rithwik Bojja", SeverityLevel.Information);
rithtc.Context.Operation.Id = riop.Telemetry.Context.Operation.Id;
ri_tc.TrackTrace(rithtc);
riop.Telemetry.ResponseCode = "200";
return View();
}
finally
{
ri_tc.StopOperation(riop);
ri_tc.Flush();
}
}
Here I have used rithtc.Context.Operation.Id = riop.Telemetry.Context.Operation.Id;
with this we can have same operation id and I have used Try, catch and finally blocks to flush it correctly.
Output:
Here you can clearly see all three have same Operation Id.