应用程序洞察不会将错误消息链接到关联的请求

时间:2017-06-20 08:17:40

标签: azure exception azure-application-insights

我正在尝试将异常链接到请求遥测但是,有时这不会发生,我已经将它提炼为简单的应用程序来举例说明问题:

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

Exception Example

正如您所看到的,我创建了一个telemetryClient,并且在我看来设置了请求的所有必需信息以及要链接的例外,两者都具有相同的操作ID,是否还有其他属性应该设置?

2 个答案:

答案 0 :(得分:2)

Cinaird上面提出的另一种方法是创建一个包含遥测的单一操作。

根据documentation,您可以使用:

// 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.

使用这种方法,一旦操作被处理,将发送请求遥测,因此您不需要finally块。此外,您在使用区块中发送的任何遥测都将与请求相关联。

答案 1 :(得分:0)

好的maby这是一个副本: How to link exceptions to requests in Application Insights on Azure?

如果没有,该线程给了我解决方案

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