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.
The key point to understand is that once the request body goes beyond ~32 KB, Azure API Management no longer provides a supported or reliable way to capture and persist the full request body inside the APIM gateway itself. This limitation applies even when the body is conditionally logged (for example, only on HTTP 400 responses). The restriction is not tied to one specific policy but comes from the APIM gateway design, memory constraints, and downstream integration limits. As a result, solutions such as Azure Monitor, Application Insights, Event Hub logging, or send-one-way-request cannot safely or consistently handle very large request payloads (1 MB–10 MB) directly from APIM. While Event Hub or Blob Storage can store large data, APIM cannot reliably forward such large payloads to them, which is the core blocker in this scenario.
Refer below points to resolve this issue or use these as workarounds:
There is no supported way for APIM to record the full request body (>32 KB) directly APIM policies can read the request body, but buffering and forwarding very large payloads (1 MB–10 MB) inside the gateway is not supported or recommended. Any approach that tries to push the entire body out of APIM (to Event Hub, email, Logic Apps, or webhooks) risks truncation, performance degradation, or gateway instability. This is a product design boundary rather than a configuration issue.
Event Hub cannot be used from APIM to save full large request bodies Although Event Hub itself can work with large messages (using batching or external storage), the APIM log-to-eventhub policy enforces a strict size limit (approximately 200 KB). APIM still has to serialize and send the payload, which means you cannot use Event Hub as an escape hatch to persist 1–10 MB request bodies directly from APIM.
The correct pattern is to store the full request body outside of APIM (backend‑side) The recommended architecture is to let the backend service handle large payload logging. When the backend detects a 400 error, it can save the full request body to Azure Blob Storage, Data Lake, or another storage service without size limitations. APIM should only pass through the request and optionally inject a correlation ID for traceability.
Use correlation IDs and metadata logging in APIM instead of full payloads from APIM, log only metadata such as correlation ID, content length, headers, request ID, and error code. The backend stores the large payload and associates it with the same correlation ID. This allows you to “find” and “check” large, failed requests without forcing APIM to store them.
For very large bodies, upload first and pass a reference through APIM If large payloads are expected (1 MB–10 MB), a robust design is:
- Client uploads the payload directly to Blob Storage.
- Client calls the API through APIM with a Blob reference (URL or ID). In case of a 400 error, the payload is already safely stored and can be reviewed without involving APIM body logging at all.
Sending large request bodies from APIM to storage, email, or webhooks is not recommended Using send-one-way-request or similar policies to forward full payloads is unsafe for large bodies and introduces risks such as gateway memory pressure, higher latency, throttling, and potential security or compliance issues. This approach is strongly discouraged for production workloads.
Summary
For request bodies larger than 32 KB (and especially 1–10 MB), there is no supported way to record the full request body directly in Azure API Management, including via Event Hub. The only reliable and scalable solution is to move full payload capture to the backend or to storage referenced by the request, while APIM is limited to correlation and metadata logging.
If the resolution was helpful, kindly take a moment to click on
and click on Yes for was this answer helpful. And, if you have any further query do let us know.