Reputation: 687
If you have a serialized exception, such as you may with a HTTP Error response, but no exception object, does anyone have an example of constructing a new ExceptionTelemetry() that Application Insights will log?
Upvotes: 2
Views: 2695
Reputation: 687
Given a serialized exception in JSON exceptionBody, I did get Application Insights to add the details with the code below. However, I don't really know what should be put in id or outerId, or how to easily fill in the List of StackFrame, or if passing the stack as a string means you don't pass the List of StackFrame
var responseAsJObject = JObject.Parse(exceptionBody);
var properties = new Dictionary<string, string>();
foreach (var pair in responseAsJObject)
{
properties.Add(pair.Key, pair.Value.ToString());
}
var measurements = new Dictionary<string, double>();
var hasFullStack = properties.TryGetValue("StackTrace", out var stack);
var edi = new ExceptionDetailsInfo
(
10000,
10000,
properties["ExceptionType"],
$"{properties["Message"]} {url}",
hasFullStack,
stack ?? string.Empty,
new List<StackFrame>()
);
var exceptionTelemetry = new ExceptionTelemetry
(
new List<ExceptionDetailsInfo>{edi},
SeverityLevel.Error,
$"HTTP Error {statusCode}",
properties,
measurements
);
telemetryClient.TrackException(exceptionTelemetry);
Upvotes: 2