简体   繁体   中英

Application Insights TrackException ExceptionTelemetry when you do not have an exception object

如果您有序列化的异常(例如您可能具有HTTP错误响应),但是没有异常对象,那么是否有人举过一个示例,该示例构造了一个新的ExceptionTelemetry(),Application Insights将记录该异常?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM