Log API Policy
Info
Logging with the Nodinite Log API policy can be used when you are using a consumption-based tier. If possible, please use the Storage Blob Policy instead of this policy.
Warning
This not the recommended solution, since there could be situations where the Nodinite Log API is not available, due to an update, maintenance window, or otherwise.
Before logging into the Nodinite Log API, make sure that it is available from your Azure API Management. It could be part of the VNET or the Log API is available on a public HTTP/HTTPS endpoint.
Policy Example
Below is all the necessary code for creating both the Inbound, and the 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 Step
Related
Azure API Management Services - Logging
JSON Log Event
Interested in logging from other Azure Related Services?