ApplicationInsights OperationId is empty

佐手、 提交于 2021-02-08 09:40:11

问题


I'm implementing custom ApplicationInsights logger and able to write all logs in write places like traces, exceptions and request, but OperationId is empty in traces and exceptions.

Yesterday I was using same code and getting OperationId in all tables. After that I was playing for multi-thread scenario which didn't work well. Now I started again with simple code but can't see OperationId.

what is wrong in my code?

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:


This issue is very tricky, it's due to the instrumentation key setting. If you use Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration(you used in your code) to set instrumentation key, then no operation_id appears in app insights.

So please use this line of code to set instrumentation key:

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

My sample code as below, only change the instrumentation key setting method:

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

After executed, you can see the operation_id for trace / exception in azure portal:



来源:https://stackoverflow.com/questions/52688906/applicationinsights-operationid-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!