question

Carlos-3787 avatar image
0 Votes"
Carlos-3787 asked JayaC-MSFT answered

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

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Carlos-3787 Thank you for the question. We will review and update at the earliest.

0 Votes 0 ·
Carlos-3787 avatar image
0 Votes"
Carlos-3787 answered Carlos-3787 edited

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"
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JayaC-MSFT avatar image
0 Votes"
JayaC-MSFT answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.