An Azure service that provides a hybrid, multi-cloud management platform for APIs.
Hi @jinxjer lee
Thank you for reaching out to Microsoft Q&A.
You’re preserving the request body in inbound and then trying to emit it on error via a <trace> call unfortunately, those <trace> messages don’t end up in the ApiManagementGatewayLogs table by default. Here are a couple of ways to capture only the 400-error payloads:
Enable built-in diagnostics for “Frontend Request” bodies
In your APIM instance, go to “Diagnostic Logs” > “Azure Monitor.”
Enable logging, check Always log errors, and under Additional settings tick Frontend Request (and optionally Backend Request).
Save.
Now every error call (400, 500, etc.) will write the request/response bodies (up to 8 KB each, and a 32 KB total entry limit) into ApiManagementGatewayLogs.
In Log Analytics, you can then run:
ApiManagementGatewayLogs
| where HttpStatusCode == 400
| project TimeGenerated, OperationName, RequestBody, ResponseBody
Use a real logger policy instead of <trace>
Define an Event Hub or Application Insights logger in APIM (under Settings > Loggers).
In your <on-error> block change your <trace> to:
<log-to-eventhub logger-id="your-eventhub-logger">
{
"status": @(context.Response.StatusCode),
"url": "@(context.Request.Url.Path)",
"body": "@(context.Variables["savedBody"])"
}
</log-to-eventhub>
Or similarly use <log-to-application-insights> if you prefer App Insights.
This will stream your error payloads off to the configured logger, bypassing the 32 KB diagnostics limit in Azure Monitor.
Adjust your error condition to catch 400s
You currently only log on StatusCode == 500. Change your <when> to:
<when condition="@(context.Response.StatusCode == 400)">
…your log-to-eventhub or trace…
</when>
Notes:
APIM enforces an 8 KB limit on logged request/response bodies and 32 KB per entry. If your payloads exceed that, they’ll get trimmed.
Traces (<trace>) show up in the Test Console but aren’t pushed into the gateway diagnostic logs table.