没有异常对象时的Application Insights TrackException ExceptionTelemetry

时间:2019-01-11 15:40:13

标签: c# azure-application-insights http-error

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

1 个答案:

答案 0 :(得分:2)

鉴于JSON exceptionBody中的序列化异常,我确实获得了Application Insights来添加以下代码的详细信息。但是,我真的不知道应该在id或externalId中放置什么,或者如何轻松地填充StackFrame的列表,或者是否将堆栈作为字符串传递表示您不传递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);