0
0
Azurecloud~5 mins

App Service diagnostics and logging in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
When your web app has problems or you want to see how it behaves, you need to collect information about its activity and errors. App Service diagnostics and logging help you capture this information so you can understand and fix issues quickly.
When your web app is not responding or crashes unexpectedly and you want to find out why.
When you want to monitor the usage and performance of your app over time.
When you need to keep a record of errors and warnings for troubleshooting.
When you want to see detailed request and response logs to analyze traffic patterns.
When you want to enable diagnostics to get automatic suggestions from Azure about your app health.
Config File - diagnostics-settings.json
diagnostics-settings.json
{
  "kind": "web",
  "properties": {
    "logs": {
      "applicationLogs": {
        "fileSystem": {
          "level": "Information"
        }
      },
      "httpLogs": {
        "fileSystem": {
          "enabled": true,
          "retentionInDays": 7
        }
      },
      "detailedErrorMessages": {
        "enabled": true
      },
      "failedRequestTracing": {
        "enabled": true
      }
    }
  }
}

This JSON configures diagnostics for an Azure App Service web app.

  • applicationLogs.fileSystem.level: Captures app logs at Information level and above.
  • httpLogs.fileSystem.enabled: Turns on logging of HTTP requests to the file system.
  • httpLogs.fileSystem.retentionInDays: Keeps logs for 7 days before deleting.
  • detailedErrorMessages.enabled: Enables detailed error pages for easier debugging.
  • failedRequestTracing.enabled: Captures detailed traces of failed requests for deep troubleshooting.
Commands
This command enables application logging to the file system, detailed error messages, failed request tracing, and web server logging with a retention period of 7 days for the Azure App Service named 'example-app' in resource group 'example-rg'.
Terminal
az webapp log config --name example-app --resource-group example-rg --application-logging filesystem --detailed-error-messages true --failed-request-tracing true --web-server-logging filesystem --retention 7
Expected OutputExpected
Updated logging configuration for webapp example-app
--application-logging - Enables application logs to be saved to the file system.
--detailed-error-messages - Enables detailed error messages for easier debugging.
--failed-request-tracing - Enables tracing of failed HTTP requests.
This command streams the live logs from the 'example-app' web app so you can watch logs in real time as the app runs.
Terminal
az webapp log tail --name example-app --resource-group example-rg
Expected OutputExpected
Connecting to log stream... [2024-06-01T12:00:00] INFO Starting up example-app [2024-06-01T12:00:05] ERROR Unhandled exception occurred [2024-06-01T12:00:10] INFO Request processed successfully
This command shows the current logging configuration for the 'example-app' web app to verify what logs are enabled.
Terminal
az webapp log show --name example-app --resource-group example-rg
Expected OutputExpected
{ "applicationLogs": { "fileSystem": { "level": "Information" } }, "detailedErrorMessages": { "enabled": true }, "failedRequestTracing": { "enabled": true }, "httpLogs": { "fileSystem": { "enabled": true, "retentionInDays": 7 } } }
Key Concept

If you remember nothing else from this pattern, remember: enabling and viewing logs helps you quickly find and fix problems in your web app.

Common Mistakes
Not enabling the file system logging before trying to view logs.
Without enabling logging, no logs are saved or streamed, so you see no useful information.
Always run the log configuration command to enable logging before trying to view or stream logs.
Setting log retention to zero or not setting it at all.
Logs get deleted immediately or too soon, losing valuable diagnostic data.
Set a reasonable retention period like 7 days to keep logs available for troubleshooting.
Trying to stream logs from a web app that is stopped or not running.
No logs are generated if the app is not running, so the log stream will be empty.
Ensure the web app is running before streaming logs.
Summary
Use 'az webapp log config' to enable application, HTTP, and error logging with retention settings.
Use 'az webapp log tail' to stream live logs and watch your app's behavior in real time.
Use 'az webapp log show' to check the current logging configuration and verify settings.