Application Insights TrackEvent only fires from constructor. Does not work in function

Carlos 6 Reputation points
2021-06-04T21:59:16.357+00:00

In the following Azure function, TrackEvent only fires events from the constructor. TrackEvent calls from TestTelemetryFunc function are never fired.

The TestTelemetryFunc function is using a cached telemetry client received in the constructor.

public class TestApi
{
    private readonly TelemetryConfiguration telemetryConfiguration;
    private readonly TelemetryClient telemetryClient;
    private readonly TelemetryClient telemetryClientFromConfig;

    public TestApi(TelemetryConfiguration telemetryConfiguration, TelemetryClient telemetryClient)
    {
        this.telemetryConfiguration = telemetryConfiguration;
        this.telemetryClient = telemetryClient;

        this.telemetryClient.TrackEvent(new EventTelemetry("Constructor.telemetryClient"));

        this.telemetryClientFromConfig = new TelemetryClient(telemetryConfiguration);
        this.telemetryClientFromConfig.TrackEvent(new EventTelemetry("Constructor.telemetryClientFromConfig"));
    }

    [FunctionName("TestTelemetryFunc")]
    public async Task TestTelemetryFunc([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "test-telemetry/site/{siteId}")] HttpRequest req, string siteId)
    {
        this.telemetryClient.TrackEvent(new EventTelemetry("TestTelemetryFunc.telemetryClient"));
        this.telemetryClientFromConfig.TrackEvent(new EventTelemetry("TestTelemetryFunc.telemetryClientFromConfig"));
    }
}

Application Insights Results

6/4/2021, 3:08:04 PM | IngestionApi.TestTelemetry.Constructor | customEvent | TestTelemetryFunc
6/4/2021, 3:08:04 PM | Constructor.telemetryClientFromConfig | customEvent | TestTelemetryFunc
6/4/2021, 3:08:04 PM | Constructor.telemetryClient | customEvent | TestTelemetryFunc
6/4/2021, 3:08:03 PM | IngestionApi.TestTelemetry.Constructor | customEvent | TestTelemetryFunc
6/4/2021, 3:08:03 PM | Constructor.telemetryClientFromConfig | customEvent | TestTelemetryFunc
6/4/2021, 3:08:03 PM | Constructor.telemetryClient | customEvent | TestTelemetryFunc
6/4/2021, 3:08:02 PM | IngestionApi.TestTelemetry.Constructor | customEvent | TestTelemetryFunc
6/4/2021, 3:08:02 PM | Constructor.telemetryClientFromConfig | customEvent | TestTelemetryFunc
6/4/2021, 3:08:02 PM | Constructor.telemetryClient | customEvent | TestTelemetryFunc
6/4/2021, 3:05:21 PM | IngestionApi.TestTelemetry.Constructor | customEvent | TestTelemetryFunc
6/4/2021, 3:05:21 PM | Constructor.telemetryClientFromConfig | customEvent | TestTelemetryFunc
6/4/2021, 3:05:21 PM | Constructor.telemetryClient | customEvent | TestTelemetryFunc
6/4/2021, 2:49:19 PM | IngestionApi telemetry client | customEvent | TestTelemetryFunc
6/4/2021, 2:49:19 PM | Constructor.telemetryClientFromConfig | customEvent | TestTelemetryFunc
6/4/2021, 2:49:19 PM | Constructor.telemetryClient | customEvent | TestTelemetryFunc

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,710 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Carlos 6 Reputation points
    2021-06-07T21:10:29.117+00:00

    Got it to work.

    Turned out the host.json settings were affecting the telemetry. Removed the entries with values of Warning below.

    "logLevel": {
      "default": "Information",
      "Function": "Warning",
      "Host": "Warning",
      "Host.Results": "Warning",
      "Host.Aggregator": "Warning",
      "Host.Executor": "Warning",
      "Microsoft.EntityFrameworkCore": "Warning"
    }
    
    0 comments No comments

  2. JayaC-MSFT 5,606 Reputation points
    2021-06-08T11:41:47.323+00:00

    Hello @Carlos , glad that the issue is resolved now. For reference , https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#configure-log-levels
    you may configure the log level for customized result.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.