- Knowledge Base
- Microsoft Azure
- API Management
- Enable Resource Logs
Ensure that your Azure API Management API services are configured to use resource logs in order to collect valuable information on API Management operations and errors. By enabling resource logs through a diagnostic setting, you can gather extensive information on the API requests received and handled by the Azure API Management service gateway.
excellence
Enabling resource logs (Azure Monitor logs) for Azure API Management services provides a wealth of insights into API operations and errors, enhancing troubleshooting, auditing, and performance monitoring capabilities. This enables administrators to proactively identify and resolve issues promptly, improve API performance, and gain valuable insights into API usage patterns.
Audit
To determine if resource logs are enabled for Azure API Management API services, perform the following actions:
Using Azure Console
-
Sign in to the Azure Management Console.
-
Navigate to All resources blade at https://portal.azure.com/#view/HubsExtension/BrowseAll to access all your Microsoft Azure cloud resources.
-
Choose the Azure subscription that you want to access from the Subscription filter box and choose Apply.
-
From the Type filter box, select API Management service and choose Apply to list only the Microsoft Azure API Management services available in the selected subscription.
-
Click on the name (link) of the Azure API Management service that you want to examine.
-
In the navigation panel, under Monitoring, select Diagnostic settings. Diagnostic settings are used to configure the streaming export of the logs and metrics to the destination of your choice.
-
Check for any diagnostic settings configured for your API service, listed in the Diagnostic settings list. If there are no diagnostic settings listed in this section and the following message is displayed: No diagnostic settings defined, resource logs are not being exported for the selected Azure API Management service instance.
-
Repeat steps no. 5 – 7 for each Azure API Management service available in the selected Azure subscription.
-
Repeat steps no. 3 – 8 for each subscription created in your Microsoft Azure cloud account.
Using Azure CLI
-
Run apim list command (Windows/macOS/Linux) using custom query filters to list the name and the associated resource group for each Azure API Management service instance available in the current subscription:
az apim list --output table --query '[*].{name:name, resourceGroup:resourceGroup}' -
The command output should return the requested Azure API Management service names:
Name ResourceGroup ---------------------- ------------------------------ cc-main-api-service-instance cloud-shell-storage-westeurope cc-project5-service-instance cloud-shell-storage-westeurope
-
Run monitor diagnostic-settings list command (Windows/macOS/Linux) with custom query filters to list the names of the diagnostic settings created and configured for the selected Azure API Management service:
az monitor diagnostic-settings list --resource cc-main-api-service-instance --resource-group cloud-shell-storage-westeurope --resource-type "Microsoft.ApiManagement/service" --query '[*].name'
-
The command output should return the identifier (name) of each diagnostic setting defined for the selected resource:
[]
If the monitor diagnostic-settings list command output returns an empty array (i.e. []), as shown in the output example above, there are no diagnostic settings configured to export resource logs for the selected Azure API Management service instance.
-
Repeat steps no. 3 and 4 for each Azure API Management service available within the current Azure subscription.
-
Repeat steps no. 1 – 5 for each subscription created in your Microsoft Azure cloud account.
Remediation / Resolution
To ensure that resource logs are enabled for your Azure API Management API services, perform the following actions:
Using Azure Console
-
Sign in to the Azure Management Console.
-
Navigate to All resources blade at https://portal.azure.com/#view/HubsExtension/BrowseAll to access all your Microsoft Azure cloud resources.
-
Choose the Azure subscription that you want to access from the Subscription filter box and choose Apply.
-
From the Type filter box, select API Management service and choose Apply to list only the Microsoft Azure API Management services available in the selected subscription.
-
Click on the name (link) of the Azure API Management service that you want to configure.
-
In the navigation panel, under Monitoring, select Diagnostic settings. Diagnostic settings are used to configure the streaming export of the logs and metrics to the destination of your choice.
-
Choose + Add diagnostic setting to create a new diagnostic setting for the selected resource.
-
On the Diagnostic setting setup page, perform the following operations:
- Provide a unique name for your new diagnostic setting in the Diagnostic setting name box.
- For Logs, choose the log categories that you want to collect from the selected resource, available in the Categories section. You can also choose to enable one or more log category groups from the Category groups section.
- For Destination details, select one of the following options or multiple options, based on your application requirements:
- Select Send to Log Analytics workspace and choose the subscription and the Log Analytics workspace that you want to use to collect your logging data. For Destination table select which table resource data is stored in.
- Select Archive to a storage account if you want to write the logs to an Azure storage account. Choose the Azure subscription and the storage account that will store your log data.
- Select Stream to an event hub if you want to write the resource logs to an Azure Event Hub. Choose the subscription and the event hub namespace in which you would like an event hub to be created for streaming your logging data.
- Select Send to partner solution for partner integration. You must first install partner integration into your Azure subscription. Configuration options vary by partner.
- Choose Save to apply the changes. Logs are streamed to the specified destination(s) as new event data is generated.
-
Repeat steps no. 5 – 8 for each Azure API Management service that you want to configure, available in the selected Azure subscription.
-
Repeat steps no. 3 – 9 for each subscription created within your Microsoft Azure cloud account.
Using Azure CLI
-
Run monitor diagnostic-settings create command (Windows/macOS/Linux) to create a new diagnostic setting for your Azure API Management service instance, in order to send resource logs to a Log Analytics workspace, a storage account, a partner solution, and/or to an Azure Event Hub. For example, the following command request creates a diagnostic setting that writes your API Management resource logs (i.e. API Management gateway and WebSocket connection logs) to an Azure storage account identified by the ID "/subscriptions/abcdabcd-abcd-abcd-abcd-abcdabcdabcd/resourceGroups/cloud-shell-storage-westeurope/providers/microsoft.Storage/storageAccounts/abcdabcdabcdabcdabcdabcd":
az monitor diagnostic-settings create --resource cc-main-api-service-instance --resource-group cloud-shell-storage-westeurope --resource-type "Microsoft.ApiManagement/service" --name cc-api-resource-diagnostic-setting --storage-account "/subscriptions/abcdabcd-abcd-abcd-abcd-abcdabcdabcd/resourceGroups/cloud-shell-storage-westeurope/providers/microsoft.Storage/storageAccounts/abcdabcdabcdabcdabcdabcd" --logs '[ { "category": "GatewayLogs", "enabled": true }, { "category": "WebSocketConnectionLogs", "enabled": true } ]' -
The command output should return the configuration information available for the new diagnostic setting:
{ "id": "/subscriptions/abcdabcd-abcd-abcd-abcd-abcdabcdabcd/resourcegroups/cloud-shell-storage-westeurope/providers/microsoft.apimanagement/service/cc-main-api-service-instance/providers/microsoft.insights/diagnosticSettings/cc-api-resource-diagnostic-setting", "logs": [ { "category": "GatewayLogs", "enabled": true, "retentionPolicy": { "days": 0, "enabled": false } }, { "category": "WebSocketConnectionLogs", "enabled": true, "retentionPolicy": { "days": 0, "enabled": false } } ], "metrics": [], "name": "cc-api-resource-diagnostic-setting", "resourceGroup": "cloud-shell-storage-westeurope", "storageAccountId": "/subscriptions/abcdabcd-abcd-abcd-abcd-abcdabcdabcd/resourceGroups/cloud-shell-storage-westeurope/providers/microsoft.Storage/storageAccounts/abcdabcdabcdabcdabcdabcd", "type": "Microsoft.Insights/diagnosticSettings" } -
Repeat steps no. 1 and 2 for each Azure API Management service that you want to configure, available within the current subscription.
-
Repeat steps no. 1 - 3 for each subscription created in your Microsoft Azure cloud account.
References
- Azure Official Documentation
- Resource logs
- Diagnostic settings in Azure Monitor
- Azure CLI Documentation
- az apim list
- az monitor diagnostic-settings list
- az monitor diagnostic-settings create
Related APIManagement rules
- Authorize Developer Accounts by Using Microsoft Entra ID (Security, operational-excellence)
- Check the Cipher Suites Configured for API Gateways (Security)
- Disable Public Network Access to API Management Services with Private Endpoints (Security)
- Use System-Assigned Managed Identities for Azure API Management Services (Security, operational-excellence)