Log API request/response payload in Azure Application Insights
Sometimes when we expose the APIs in Azure API Management (APIM) there will be situations were we face issues when the API fails and we need to log the incoming payload and possibly log the outgoing payloads as well.
Fortunately APIM provides an easy way to do this.
Steps to log
- Login into the Azure portal and navigate to the specific API Management resource
- Go to settings
- Select the specific API
- On the right hand side, select the settings
- Navigate to Diagnostics Logs
- Check the ‘Enable’ checkbox under Application Insights
- Select the Default sampling rate to 100%
- Set the verbosity to Information
- Set Correlation protocol as W3C
- Go to Advanced Options under Additional settings
- Check the Frontend Request and Frontend Response
- Under the setting for both the Request & Response
- Add “X-Forwarded-For” under the headers
- Add “8192” under the Number of payload bytes to log
- Save the changes
- Navigate to the App Insights
- Open the Live Metrics feature
- Allow the API to be hit
- Go to the logs
- Filter using the API URL
- In the results section under the request body node, you should be able to see the request body
- The same for the response body can be used.
You can also use a query like below to filter the App Insight logs
requests
| where url == <API URL>"
| where timestamp >= ago (1h)
| order by timestamp desc