ApplicationInsights操作ID为空

3

我正在实现自定义的ApplicationInsights日志记录器,能够将所有日志写入正确的位置,如跟踪、异常和请求,但是在跟踪和异常中OperationId为空。

昨天我使用相同的代码,在所有表格中获取到了OperationId。之后我尝试了多线程方案,但效果不佳。现在我重新开始使用简单的代码,但无法看到OperationId。

我的代码有什么问题?

public static class Function2
{
    private static TelemetryClient telemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
    {
        InstrumentationKey = "********-****-********-****"
    });

    [FunctionName("Function2")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req)
    {
        RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "Function2" };
        var operation = telemetryClient.StartOperation(requestTelemetry);

        telemetryClient.TrackTrace("trace message", SeverityLevel.Error);
        telemetryClient.TrackException(new System.Exception("My custom exception"));


        operation.Telemetry.Success = true;
        telemetryClient.StopOperation(operation);

        return req.CreateResponse(HttpStatusCode.OK, "Hello ");
    }
}

请尝试下面的答案。这是一个ikey配置问题。 - Ivan Glasenberg
1个回答

8

这个问题非常棘手,它是由于仪表键设置引起的。

如果您在代码中使用Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration来设置仪表键,则应用程序洞察中不会出现任何操作ID。

因此,请使用以下代码行来设置仪表键:

TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };

以下是我的示例代码,仅更改了仪表盘密钥设置方法:

public static class Function1
{

    private static TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };

    [FunctionName("Function2")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req)
    {
        RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "Function211" };
        var operation = telemetryClient.StartOperation(requestTelemetry);

        telemetryClient.TrackTrace("trace message 111", SeverityLevel.Error);
        telemetryClient.TrackException(new System.Exception("My custom exception 111"));

        operation.Telemetry.Success = true;
        telemetryClient.StopOperation(operation);

        return req.CreateResponse(HttpStatusCode.OK, "Hello ");
    }
}

执行后,您可以在Azure门户中看到追踪/异常的操作ID:enter image description here

谢谢@Ivan,微软应该解决这个问题。 - Pankaj Rawat
1
@PankajRawat,我已经提交了这个问题。如果有任何反馈,我会让你知道 :)。 - Ivan Glasenberg
@IvanYang,你有那个问题的链接吗? - Peter Bons
@PeterBons,你可以在Github上查看这个问题 - Ivan Glasenberg
1
哇,到了2020年,这仍然是一个问题。根据您如何初始化仪表板密钥,跟踪会以不同的方式进行跟踪。我已经四处搜索,下载了源代码,但无法找出为什么我的“TrackTrace”没有携带任何上下文。 - Charles Chen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接