Reputation: 785
I am trying to link exceptions to request telemetry but, sometimes this does not happen, I have distilled it to simple app to exemplify the problem:
static void Main(string[] args)
{
TelemetryConfiguration.Active.InstrumentationKey = _iKey;
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
var tc = new TelemetryClient();
tc.Context.Operation.Id = Guid.NewGuid().ToString();
tc.Context.Operation.Name = "Test Telemetry";
try
{
var a = 0;
var b = 2 / a;
}
catch (Exception e)
{
tc.TrackException(new Exception("Triggered error", e));
}
finally
{
tc.TrackRequest("Request", DateTimeOffset.UtcNow, new TimeSpan(0, 0, 1), "500", false);
tc.Flush();
}
}
As you can see I create a telemetryClient and in my opinion sets all the required information for the request and the exception to be linked, both have the same operational ID, is there any other property that should have been set?
Upvotes: 0
Views: 499
Reputation: 554
A different approach then what was proposed by Cinaird above, is to create a single operation that will wrap your telemetry.
Based on the documentation, you can use:
// Establish an operation context and associated telemetry item:
using (var operation = telemetry.StartOperation<RequestTelemetry>("operationName"))
{
// Telemetry sent in here will use the same operation ID.
...
telemetry.TrackEvent(...); // or other Track* calls
...
// Set properties of containing telemetry item--for example:
operation.Telemetry.ResponseCode = "200";
// Optional: explicitly send telemetry item:
telemetry.StopOperation(operation);
} // When operation is disposed, telemetry item is sent.
Using this approach, the request telemetry will be sent once the operation is disposed, so you don't need the finally block. Also, any telemetry you send in the using block will be correlated to the request.
Upvotes: 2
Reputation: 785
Ok maby this is a duplicate of this: How to link exceptions to requests in Application Insights on Azure?
If not, that thread gave me the solution
static void Main(string[] args)
{
TelemetryConfiguration.Active.InstrumentationKey = _iKey;
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
var tc = new TelemetryClient();
tc.Context.Operation.Id = Guid.NewGuid().ToString();
tc.Context.Operation.Name = "Test Telemetry";
var rt = new RequestTelemetry("Request", DateTimeOffset.UtcNow, new TimeSpan(0, 0, 1), "500", false);
ExceptionTelemetry exceptionTelemetry = null;
try
{
var a = 0;
var b = 2 / a;
}
catch (Exception e)
{
exceptionTelemetry = new ExceptionTelemetry(e);
exceptionTelemetry.Context.Operation.Id = rt.Id;
tc.TrackException(exceptionTelemetry);
}
finally
{
tc.TrackRequest(rt);
//tc.TrackRequest("Request", DateTimeOffset.UtcNow, new TimeSpan(0, 0, 1), "500", false);
tc.Flush();
}
}
Upvotes: 0