Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApplicationInsights OperationId is empty

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 ");
    }
}
like image 690
Pankaj Rawat Avatar asked Oct 22 '25 06:10

Pankaj Rawat


1 Answers

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: enter image description here

like image 98
Ivan Yang Avatar answered Oct 24 '25 18:10

Ivan Yang