Denis Molodtsov
Denis Molodtsov

Reputation: 824

ILogger does not log to Application Insights when using Azure Function V2 Durable functions

Can someone, please explain how to force ILogger to actually log something into Application insights when using Azure Function V2 (.net Core)?

We have Application Insights configured and it shows live telementry and unhandled exceptions. What we are trying to make work is to force Application insights to store logs we create via the default ILogger.

No matter what kind of logLevel we are using (Information, Warning, Error, Critical) - nothing gets stored in Application insights.

I have also tried creating 500 log messages in the hopes that it might push it to application insights as a batch.

Am I missing something obvious here? Can someone suggest how to use the default ILogger so that it passes something to the Application Insights associated with the Azure function App V2 (.net core)?

host.json

{
  "version": "2.0",
  "functionTimeout": "00:10:00",
  "extensions": {
    "durableTask": {
      "maxConcurrentActivityFunctions": 4,
      "maxConcurrentOrchestratorFunctions": 1
    }
  },
  "logging": {
    "fileLoggingMode": "debugOnly",
    "logLevel": {
      "default": "Information"
    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "maxTelemetryItemsPerSecond": 5
      }
    }
  }
}

Azure Function V2, TimerTrigger, DurableOrchestrationClient:

[FunctionName("MainTriggerEntry")]
public static async Task RunMainTriggerEntry([TimerTrigger("%CRON_EXPRESSION%", RunOnStartup = false)]TimerInfo timer,
[OrchestrationClient]DurableOrchestrationClient starter, ILogger log)
{
    log.LogInformation("[Information] Message!");
    log.LogError("[Error]. Something occured");
    log.LogCritical("[Critical] Critical issue");
    for (var i = 0; i < 500; i++)
    {
        log.LogWarning($"[Warning] Logging to Application insights. {i}");
    }

    // We don't want more than one orchestrator running at the same time:
    var orchestrationStatus = await starter.GetStatusAsync(OrchestratorInstanceGuid);
    if (orchestrationStatus == null || !orchestrationStatus.IsBusy())
    {
        var instanceId = await starter.StartNewAsync(OrchestratorFunctionName, OrchestratorInstanceGuid, null);
        log.LogInformation($"Triggering {OrchestratorFunctionName} function with an ID '{instanceId}'.");
    }
    else
    {
        log.LogInformation($"{OrchestratorFunctionName} function with an ID '{OrchestratorInstanceGuid}' is already running.");
    }
}

Nothing shows up in Application insights that we logged. But Failures show up as they should: enter image description here

This shows that ILogger saves something on disk: enter image description here

More info:

Upvotes: 6

Views: 4903

Answers (1)

Peter Bons
Peter Bons

Reputation: 29840

The activity log is not the place you want to look for your logs. The logs written using Ilogger are stored as Traces in application insights. You can query them using the Search menu item (the option directly above the Availability menu item in your 2nd screenshot)

The activity log will show you events regarding the application insights resource itself, not the data it contains.

Upvotes: 9

Related Questions