Enable diagnostic logging

Completed

There are built-in diagnostics to assist with debugging an App Service app. In this lesson, you learn how to enable diagnostic logging and add instrumentation to your application, and how to access the information logged by Azure.

The following table shows the types of logging, the platforms supported, and where the logs can be stored and located for accessing the information.

Type Platform Location Description
Application logging Windows, Linux App Service file system and/or Azure Storage blobs Logs messages generated by your application code. The messages are generated by the web framework you choose, or from your application code directly using the standard logging pattern of your language. Each message is assigned one of the following categories: Critical, Error, Warning, Info, Debug, and Trace.
Web server logging Windows App Service file system or Azure Storage blobs Raw HTTP request data in the W3C extended log file format. Each log message includes data like the HTTP method, resource URI, client IP, client port, user agent, response code, and so on.
Detailed error logging Windows App Service file system Copies of the .html error pages that would have been sent to the client browser. For security reasons, detailed error pages shouldn't be sent to clients in production, but App Service can save the error page each time an application error occurs that has HTTP code 400 or greater.
Failed request tracing Windows App Service file system Detailed tracing information on failed requests, including a trace of the IIS components used to process the request and the time taken in each component. One folder is generated for each failed request, which contains the XML log file, and the XSL stylesheet to view the log file with.
Deployment logging Windows, Linux App Service file system Helps determine why a deployment failed. Deployment logging happens automatically and there are no configurable settings for deployment logging.

Enable application logging (Windows)

  1. To enable application logging for Windows apps in the Azure portal, navigate to your app and select App Service logs.

  2. Select On for either Application Logging (Filesystem) or Application Logging (Blob), or both. The Filesystem option is for temporary debugging purposes, and turns itself off in 12 hours. The Blob option is for long-term logging, and needs a blob storage container to write logs to.

    Note

    If you regenerate your storage account's access keys, you must reset the respective logging configuration to use the updated access keys. To do this turn the logging feature off and then on again.

  3. You can also set the Level of details included in the log as shown in the following table.

    Level Included categories
    Disabled None
    Error Error, Critical
    Warning Warning, Error, Critical
    Information Info, Warning, Error, Critical
    Verbose Trace, Debug, Info, Warning, Error, Critical (all categories)
  4. When finished, select Save.

Enable application logging (Linux/Container)

  1. In App Service logs set the Application logging option to File System.

  2. In Quota (MB), specify the disk quota for the application logs. In Retention Period (Days), set the number of days the logs should be retained.

  3. When finished, select Save.

Enable web server logging

  1. For Web server logging, select Storage to store logs on blob storage, or File System to store logs on the App Service file system.

  2. In Retention Period (Days), set the number of days the logs should be retained.

  3. When finished, select Save.

Add log messages in code

In your application code, you use the usual logging facilities to send log messages to the application logs. For example:

  • ASP.NET applications can use the System.Diagnostics.Trace class to log information to the application diagnostics log. For example:

    System.Diagnostics.Trace.TraceError("If you're seeing this, something bad happened");
    
  • By default, ASP.NET Core uses the Microsoft.Extensions.Logging.AzureAppServices logging provider.

  • Python applications can use the OpenCensus package to send logs to the application diagnostics log.

Stream logs

Before you stream logs in real time, enable the log type that you want. Any information written to files ending in .txt, .log, or .htm that are stored in the /LogFiles directory (d:/home/logfiles) is streamed by App Service.

Note

Some types of logging buffer write to the log file, which can result in out of order events in the stream. For example, an application log entry that occurs when a user visits a page may be displayed in the stream before the corresponding HTTP log entry for the page request.

  • Azure portal - To stream logs in the Azure portal, navigate to your app and select Log stream.

  • Azure CLI - To stream logs live in Cloud Shell, use the following command:

    az webapp log tail --name appname --resource-group myResourceGroup
    
  • Local console - To stream logs in the local console, install Azure CLI and sign in to your account. Once signed in, follow the instructions shown for Azure CLI.

Access log files

If you configure the Azure Storage blobs option for a log type, you need a client tool that works with Azure Storage.

For logs stored in the App Service file system, the easiest way is to download the ZIP file in the browser at:

  • Linux/container apps: https://<app-name>.scm.azurewebsites.net/api/logs/docker/zip
  • Windows apps: https://<app-name>.scm.azurewebsites.net/api/dump

For Linux/container apps, the ZIP file contains console output logs for both the docker host and the docker container. For a scaled-out app, the ZIP file contains one set of logs for each instance. In the App Service file system, these log files are the contents of the /home/LogFiles directory.