Diagnostics in Azure API Management

Overview

Diagnostics enable you to capture request and response details for APIs managed by Azure API Management (APIM). Use diagnostics to troubleshoot, monitor performance, and gain insights into traffic patterns.

Enable Diagnostics

Diagnostics are configured per API, operation, or globally at the service level. You can direct logs to Azure Monitor, Application Insights, or a custom logger.


{
  "properties": {
    "diagnostics": [
      {
        "enabled": true,
        "loggerId": "appInsightsLogger",
        "sampling": {
          "samplingType": "Fixed",
          "percentage": 100
        },
        "alwaysLog": "allErrors",
        "frontend": {
          "request": {
            "body": { "bytes": 2048 }
          },
          "response": {
            "body": { "bytes": 2048 }
          }
        },
        "backend": {
          "request": {
            "body": { "bytes": 2048 }
          },
          "response": {
            "body": { "bytes": 2048 }
          }
        }
      }
    ]
  }
}
        

Create a Logger

Before enabling diagnostics, create a logger that points to your destination.


az apim logger create --resource-group MyResourceGroup \
    --service-name MyAPIMService \
    --logger-id appInsightsLogger \
    --application-insights /subscriptions/xxxx/resourceGroups/xxxx/providers/microsoft.insights/components/MyAppInsights \
    --description "App Insights logger for diagnostics"
        

Policy Example: Log Request/Response

Add the <log-to-eventhub> or <log-to-applicationinsights> policy within the <inbound> and <outbound> sections.


<policies>
  <inbound>
    <base/>
    <log-to-applicationinsights 
        logger-id="appInsightsLogger"
        sampling-rate="100"/>
  </inbound>
  <backend>
    <base/>
  </backend>
  <outbound>
    <base/>
    <log-to-applicationinsights 
        logger-id="appInsightsLogger"
        sampling-rate="100"/>
  </outbound>
</policies>
        

Diagnostic Settings Summary

NameEnabledLoggerSamplingScope
GlobalDiagnosticsYesappInsightsLogger100%Service
OrdersAPIYesappInsightsLogger50%API
GetOrderOperationNo--Operation

Best Practices