Unable to see Invocation logs for App Service functions

Viewed 2414

When I publish function code (Runtime version: 3.0.14287.0 (~3) to an App Service, everything appears to be running but I cannot see any invocation logs in the Monitor page of any functions in the portal - it's always blank. I have timer-triggered functions, which should show up.

I am using an ARM script to deploy a resource group that contains an App Service. The deployment consists of the following resources:

Microsoft.Insights/components
Microsoft.KeyVault/vaults
Microsoft.Storage/storageAccounts
Microsoft.Web/serverfarms
Microsoft.Web/sites

I can see the function traces in Application Insights using something like this query:

traces 
| order by timestamp desc
| where message contains 'telemetry'

I don't know if it is related, but in the App Service Overview page, I do see the following warning:

Storage is not configured properly, Function scaling will be limited. Click to learn more.

I'm using the following App Service appsettings to connect it to App Insights:

APPINSIGHT_INSTRUMENTATIONKEY: <instrumentation key from app insights>
AzureWebJobsDashboard: <storage acct connect string>
AzureWebJobsStorage: <storage acct  connect string>
FUNCTIONS_EXTENSION_VERSION ~3
FUNCTIONS_WORKER_RUNTIME dotnet

The storage account has the following objects, that were created automatically.

BLOB <name>-leases
BLOB azure-webjobs-hosts
BLOB azure-webjobs-secrets
QUEUES <name>-control-[00,01,02,03]
QUEUES <name>-workitems
TABLE <name>History
TABLE <name>Instances
TABLE AzureWebJobsHostLogs202008
TABLE AzureWebJobsHostLogscommon

Here's my hosts.json:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Debug",
      "Host.Results": "Error",
      "Function": "Trace",
      "Host.Aggregator": "Trace"
    }
  }
}
3 Answers

In the file host.json, for the filed "Function", set its value to Trace. Then it can be logged into application insights.

Sample host.json for Azure function v2, which can log trace messages to Application Insights:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information",
      "Host.Results": "Error",
      "Function": "Trace",
      "Host.Aggregator": "Trace"
    }
  }
}

In Azure portal, navigate to your function app -> in the function app settings, then change log level to trace in the host.json.

enter image description here

For more details, refer to this article on how to set log level for function v1 or v2.

Note: the invocation log in app insight and Monitor has delay time. After doing operation, wait for 5 minutes and check again. You can go the app insight check first. If app insight does not have log, Monitor will not have.

I believe this configuration:

"Host.Results": "Error"

in your host.json logLevel settings is the problem.

This setting controls the logging of requests.

"These runtime-generated logs indicate success or failure of a function. All of these logs are written at Information level. If you filter at Warning or above, you won't see any of this data."

See: https://docs.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2

Try setting this to 'Trace' and I expect you'll see invocation logs for all requests (and not just errors).

I found that explicitly disabling the application insights sampling settings in the host.json fixed this issue

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false
      }
    },
    "logLevel": {
      "default": "Debug",
      "Host.Results": "Error",
      "Function": "Trace",
      "Host.Aggregator": "Trace",
    }
  }
}

Related