Log API Policy
Section titled “Log API Policy”Unlock direct, policy-based logging for Azure API Management with Nodinite. This guide shows you how to use the Log API Policy, when to choose it, and how to implement it for full request and response tracking—even in consumption-based tiers.
✅ Enable logging for consumption-based Azure API Management tiers
✅ Track all requests and responses, including payloads and headers
✅ Use a direct, policy-based approach for flexible integration
✅ Get a complete, ready-to-use policy example
Before you log to the Nodinite Log API, ensure you can access it from your Azure API Management instance. You can expose the Log API as part of your VNET or on a public HTTP/HTTPS endpoint; see the firewall settings for Azure API Management logging.
Diagram: Direct logging from Azure API Management to Nodinite Log API.
Policy Example
Section titled “Policy Example”Use the following code to create both Inbound and Outbound Nodinite JSON Log Event.
<policies> <inbound> <!-- creating a tracking/correlation id --> <set-variable name="clientTrackingId" value="@{ return Guid.NewGuid().ToString(); }" /> <!-- if x-ms-client-tracking-id exists use it, if not, use clientTrackingId --> <set-header name="x-ms-client-tracking-id" exists-action="skip"> <value>@(context.Variables.GetValueOrDefault<string>("clientTrackingId"))</value> </set-header> <!-- Send request --> <send-request mode="new" response-variable-name="nodiniteRequestResponse" timeout="60" ignore-error="true"> <set-url>@("{Your public Nodinite log api url}")</set-url> <set-method>POST</set-method> <set-header name="content-type" exists-action="override"> <value>application/json</value> </set-header> <set-body>@{
var body = context.Request.Body.As<string>(preserveContent: true); // in case we need it later...
var bodyToLog = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(body));
var headers = context.Request.Headers .Where(h => h.Key != "Authorization" && h.Key != "Ocp-Apim-Subscription-Key");
var correlationId = context.Variables.GetValueOrDefault<string>("correlationId");
Dictionary<string, string> contextProperties = new Dictionary<string, string>(); contextProperties.Add("CorrelationId", context.Variables.GetValueOrDefault<string>("clientTrackingId")); foreach (var h in headers) { contextProperties.Add(string.Format("Header#{0}", h.Key), String.Join(", ", h.Value)); }
var requestLogMessage = new { LogAgentValueId = 15, EndPointName = context.Deployment.ServiceName + "/" + context.Api.Name + "/" + context.Operation.Name, EndPointUri = context.Deployment.ServiceName + "/" + context.Api.Name + "/" + context.Operation.Name, EndPointDirection = 0, EndPointTypeId = 71, EventDirection=21, OriginalMessageTypeName = "SupplyChain.Schemas/1.0#Orders", LogDateTime = DateTime.UtcNow, ApplicationInterchangeId = correlationId, Context = contextProperties, LogText = "Forward the API Request to the Logic App", Body = bodyToLog };
return JsonConvert.SerializeObject(requestLogMessage); }</set-body> </send-request> </inbound> <backend> <forward-request /> </backend> <outbound> <set-header name="x-ms-client-tracking-id" exists-action="override"> <value>@(context.Variables.GetValueOrDefault<string>("clientTrackingId"))</value> </set-header> <send-request mode="new" response-variable-name="nodiniteRequestResponse" timeout="60" ignore-error="true"> <set-url>@("{Your public Nodinite log api url}")</set-url> <set-method>POST</set-method> <set-header name="content-type" exists-action="override"> <value>application/json</value> </set-header> <set-body>@{ var body = context.Response.Body.As<string>(preserveContent: true); // in case we need it later...
var bodyToLog = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(body));
var headers = context.Response.Headers .Where(h => h.Key != "Authorization" && h.Key != "Ocp-Apim-Subscription-Key");
Dictionary<string, string> contextProperties = new Dictionary<string, string>(); var correlationId = context.Variables.GetValueOrDefault<string>("correlationId");
contextProperties.Add("CorrelationId", context.Variables.GetValueOrDefault<string>("clientTrackingId")); contextProperties.Add("StatusCode", context.Response.StatusCode.ToString()); foreach (var h in headers) { contextProperties.Add(string.Format("Header#{0}", h.Key), String.Join(", ", h.Value)); }
string logText = "";
if (context.Response.StatusCode > 300) { logText = "Guru meditation"; } var requestLogMessage = new { LogAgentValueId = 15, EndPointName = context.Deployment.ServiceName + "/" + context.Api.Name + "/" + context.Operation.Name, EndPointUri = context.Deployment.ServiceName + "/" + context.Api.Name + "/" + context.Operation.Name, EndPointDirection = 1, EndPointTypeId = 71, EventDirection=25, OriginalMessageTypeName = "Azure.ApiManagement.Response", LogDateTime = DateTime.UtcNow, ApplicationInterchangeId = correlationId, Context = contextProperties, LogStatus = context.Response.StatusCode, Body = bodyToLog, LogText = logText };
return JsonConvert.SerializeObject(requestLogMessage); }</set-body> </send-request> </outbound> <on-error /></policies>Next Steps
Section titled “ Next Steps”Related Topics
Section titled “ Related Topics” Azure API Management Services - Logging
JSON Log Event
Interested in logging from other Azure Related Services?