Azure Functions Intermediate

Logging and monitoring with Application Insights

When a function fails at 3 a.m. you need to see what happened. Azure Functions integrates with Application Insights for logs, requests, dependencies, failures and live metrics, and you control what is collected.

Telemetry from a function app
  1. Your codeILogger messages with structured fields.
  2. Functions hostInvocations, durations, triggers and host logs.
  3. Application InsightsRequests, traces, dependencies, exceptions.
  4. Alerts and dashboardsFailure, latency and poison-queue alerts.

Log levels and sampling decide what you keep and what you pay.

Set up Application Insights

Set APPLICATIONINSIGHTS_CONNECTION_STRING on the app (portal creation does it for you). In the isolated worker, also add the worker packages so logs from your code go straight to Application Insights.

C#
builder.Services
    .AddApplicationInsightsTelemetryWorkerService()
    .ConfigureFunctionsApplicationInsights();

// The Application Insights SDK adds a filter that keeps only Warning and above.
// Remove it if you want Information logs from your own code.
builder.Logging.Services.Configure<LoggerFilterOptions>(options =>
{
    var rule = options.Rules.FirstOrDefault(r => r.ProviderName ==
        "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
    if (rule is not null) options.Rules.Remove(rule);
});

Structured logs

Use message templates, not string interpolation, so OrderId becomes a field you can query in Kusto.

C#
logger.LogInformation("Charged {Amount} for order {OrderId}", amount, orderId);

// In Application Insights > Logs:
// traces | where customDimensions.prop__OrderId == "1042"

Control the volume

  • host.json logging.logLevel sets levels per category for the host, such as Host.Results or Function.
  • Sampling is on by default to keep costs down; exclude Request and Exception types from sampling if you need every failure.
  • Never log secrets, tokens or full personal data.
  • Set a daily cap on the Log Analytics workspace so a logging bug cannot run up a large bill.

Know when it breaks

Create alerts for failed executions, high duration, and messages in poison or dead-letter queues.

Use Live Metrics while you deploy, and the Failures and Performance views to find slow dependencies.

OpenTelemetry is also supported if you want to send telemetry to other tools as well as, or instead of, Application Insights.

Interview question

Your function's Information logs do not appear in Application Insights. Why?

In the isolated worker, the Application Insights SDK adds a default logging filter that only keeps Warning and above. I remove that rule in Program.cs or configure log levels explicitly. I also check host.json log levels, that the connection string setting is present, and that sampling is not hiding the entries.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.