---
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 :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  

>[!INFO]
>Use the :Nodinite: [Log API][] policy to log when you run a consumption-based tier. If possible, choose the [Storage Blob Policy][] for more robust and reliable logging.

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][APIM firewall].

>[!WARNING]
>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.

<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** :Nodinite: [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>
```

>[!TIP]
>APIM adds diagnostic headers such as `Ocp-Apim-Trace-AuthorizationExpired` to responses. These are captured by `context.Response.Headers` in the outbound section and will appear as context properties in :Nodinite: logs. See [APIM Troubleshooting][] if you want to understand or suppress them.

---

## :fa-hand-point-right: Next Steps

:fa-magnifying-glass-plus: [Search Fields][]  
:fa-search: [Log Views][]  

## :fa-link-horizontal: Related Topics

:fa-cloud-arrow-down: [Azure API Management Services - Logging][]  
:fa-file-code: [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]:{ndr}/Logging%20and%20Monitoring/Azure%20API%20Management/Overview.md#fa-shield-check-what-firewall-settings-are-required-for-azure-api-management-logging
[APIM Troubleshooting]:{ndr}/Logging%20and%20Monitoring/Azure%20API%20Management/Troubleshooting/Overview.md
[Azure API Management Services - Logging]:{ndr}/Logging%20and%20Monitoring/Azure%20API%20Management/Overview.md
[Event Hub Policy]:{ndr}/Logging%20and%20Monitoring/Azure%20API%20Management/Policy/Event%20Hub.md
[JSON Log Event]:{ndr}/Core%20Services/Log%20Event/Features/JSON%20Log%20Event.md
[Log API]:{ndr}/Core%20Services/Log%20API/Overview.md
[Log Views]:{ndr}/Administration/1.%20Log%20Management/1.%20Log%20Views/Features/What%20is%20a%20Log%20View.md
[LogicAppsAgent]:{ndr}/Logging%20and%20Monitoring/Azure%20Logic%20Apps/Overview.md
[Search Fields]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/What%20is%20a%20Search%20Field.md
[Serilog]:{ndr}/Logging%20and%20Monitoring/Serilog/Overview.md
[Storage Blob Policy]:{ndr}/Logging%20and%20Monitoring/Azure%20API%20Management/Policy/Blob%20Storage.md