---
title: "Log API Policy for Azure API Management: Direct Logging with Nodinite"
description: Learn how to use the Log API Policy for Azure API Management with Nodinite. This guide explains when to use the Log API, its unique benefits, and provides a complete policy example for logging requests and responses.
---

# Log API Policy

Unlock direct, policy-based logging for Azure API Management with <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span>. 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  

<aside aria-label="Info" class="nodinite-note nodinite-note--info">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-square-info"></i> Info</p>
<div class="nodinite-note__content"><p>Use the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> <a href="https://docs.nodinite.com/core-services/log-api/overview/">Log API</a> policy to log when you run a consumption-based tier. If possible, choose the <a href="https://docs.nodinite.com/logging-and-monitoring/azure-api-management/policy/blob-storage/">Storage Blob Policy</a> for more robust and reliable logging.</p></div>
</aside>

Before you log to the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [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][APIM firewall].

<aside aria-label="Warning" class="nodinite-note nodinite-note--warning">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-triangle-exclamation"></i> Warning</p>
<div class="nodinite-note__content"><p>Do not use this solution as your primary option. The Nodinite Log API may become unavailable during updates, maintenance, or outages. Always verify availability before logging.</p></div>
</aside>

<div class="mermaid">
graph LR
    A[Azure API Management] --> | Log Event | B(Nodinite's Log API)  
</div>

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

## Policy Example

Use the following code to create both **Inbound** and **Outbound** <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [JSON Log Event][].

```xml
<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>
```

<aside aria-label="Tip" class="nodinite-note nodinite-note--tip">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-lightbulb"></i> Tip</p>
<div class="nodinite-note__content"><p>APIM adds diagnostic headers such as <code>Ocp-Apim-Trace-AuthorizationExpired</code> to responses. These are captured by <code>context.Response.Headers</code> in the outbound section and will appear as context properties in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> logs. See <a href="https://docs.nodinite.com/logging-and-monitoring/azure-api-management/troubleshooting/overview/">APIM Troubleshooting</a> if you want to understand or suppress them.</p></div>
</aside>

---

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

<i class="fal fa-magnifying-glass-plus" aria-hidden="true"></i> [Search Fields][]  
<i class="fal fa-search" aria-hidden="true"></i> [Log Views][]  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

<i class="fal fa-cloud-arrow-down" aria-hidden="true"></i> [Azure API Management Services - Logging][]  
<i class="fal fa-file-code" aria-hidden="true"></i> [JSON Log Event][]  

* [Event Hub Policy][]  
* [Storage Blob Policy][]  

Interested in logging from other Azure Related Services?

* [Logging from Azure Logic Apps][LogicAppsAgent]  
* [Logging from Azure functions][Serilog]  

[APIM firewall]:https://docs.nodinite.com/logging-and-monitoring/azure-api-management/overview/#fa-shield-check-what-firewall-settings-are-required-for-azure-api-management-logging
[APIM Troubleshooting]:https://docs.nodinite.com/logging-and-monitoring/azure-api-management/troubleshooting/overview/
[Azure API Management Services - Logging]:https://docs.nodinite.com/logging-and-monitoring/azure-api-management/overview/
[Event Hub Policy]:https://docs.nodinite.com/logging-and-monitoring/azure-api-management/policy/event-hub/
[JSON Log Event]:https://docs.nodinite.com/core-services/log-event/features/json-log-event/
[Log API]:https://docs.nodinite.com/core-services/log-api/overview/
[Log Views]:https://docs.nodinite.com/administration/log-management/log-views/features/what-is-a-log-view/
[LogicAppsAgent]:https://docs.nodinite.com/logging-and-monitoring/azure-logic-apps/overview/
[Search Fields]:https://docs.nodinite.com/administration/log-management/search-fields/features/what-is-a-search-field/
[Serilog]:https://docs.nodinite.com/logging-and-monitoring/serilog/overview/
[Storage Blob Policy]:https://docs.nodinite.com/logging-and-monitoring/azure-api-management/policy/blob-storage/