ApplicationInsights OperationId为空

时间:2018-10-07 13:20:52

标签: c# azure azure-application-insights

我正在实现自定义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 ");
    }
}

1 个答案:

答案 0 :(得分:2)

此问题非常棘手,这是由于检测键设置所致。 如果您使用Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration(您在代码中使用过)来设置检测键,则应用见解中不会显示operation_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门户中看到跟踪/异常的operation_id: enter image description here