Skip to content
Documentation
Updated 6 minutes to read
Markdown

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.

graph LR A[Azure API Management] --> | Log Event | B(Nodinite's Log API)

Diagram: Direct logging from Azure API Management to Nodinite Log API.

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>

Search Fields
Log Views

Azure API Management Services - Logging
JSON Log Event

Interested in logging from other Azure Related Services?

...

Version:
Release date: N/A
Loading...

Explore Nodinite for 30 days – completely free

  • Find out why this is the leading O&M software
  • Prove your business case
  • Test the self-service for the business side

Loading...

See how Nodinite resolves your technical issues, in a LIVE Demo!

Once you submit this request:

  • We will give you a short call to understand the issues you want to tackle.
  • Understand solutions you have tried and why were you not happy with them.
  • Book a convenient slot for a LIVE demo!

Loading...