C# ApplicationInsights操作ID为空

C# ApplicationInsights操作ID为空,c#,azure,azure-application-insights,C#,Azure,Azure Application Insights,我正在实现自定义ApplicationInsights logger,能够在跟踪、异常和请求等写入位置写入所有日志,但在跟踪和异常中OperationId为空 昨天,我使用了相同的代码,并在所有表中获取OperationId。在那之后,我在玩多线程场景,但效果不好。现在我用简单的代码重新开始,但看不到OperationId 我的代码有什么问题 public static class Function2 { private static TelemetryClient telemetryC

我正在实现自定义ApplicationInsights logger,能够在跟踪、异常和请求等写入位置写入所有日志,但在跟踪和异常中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 ");
    }
}

这个问题非常棘手,这是由于仪表键设置造成的。 如果您在代码中使用Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration设置检测密钥,则app insights中不会显示任何操作id

因此,请使用这行代码设置检测键:

TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };
我的示例代码如下,仅更改instrumentation键设置方法:

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 portal中看到跟踪/异常的操作\u id:

此问题非常棘手,这是由于仪表键设置造成的。 如果您在代码中使用Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration设置检测密钥,则app insights中不会显示任何操作id

因此,请使用这行代码设置检测键:

TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = "your_key" };
我的示例代码如下,仅更改instrumentation键设置方法:

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 portal中看到跟踪/异常的操作\u id:

请尝试下面我的答案。这是一个ikey配置问题。请尝试下面我的答案。这是一个ikey配置问题。谢谢@Ivan,Microsoft应该解决这个问题。@PankajRawat,我已经提交了这个问题。如果有任何反馈,我会让你知道:@IvanYang你有关于这个问题的链接吗?@PeterBons,你可以在github上看看。哇,到2020年,这仍然是一个问题。根据您初始化指令插入键的方式,跟踪方式会有所不同。到处搜索,下载了源代码,无法找到我的TrackTrace没有任何上下文的原因。谢谢@Ivan,Microsoft应该解决这个问题。@PankajRawat,我已经提交了这个问题。如果有任何反馈,我会让你知道:@IvanYang你有关于这个问题的链接吗?@PeterBons,你可以在github上看看。哇,到2020年,这仍然是一个问题。根据您初始化指令插入键的方式,跟踪方式会有所不同。到处搜索,下载了源代码,却找不到我的TrackTrace没有任何上下文的原因。