Skip to content

Instantly share code, notes, and snippets.

@svenmalvik
Last active May 27, 2022 08:31
Show Gist options
  • Save svenmalvik/f86a80e252fe502cb55d3f4fa97d4e08 to your computer and use it in GitHub Desktop.
Save svenmalvik/f86a80e252fe502cb55d3f4fa97d4e08 to your computer and use it in GitHub Desktop.
Logging all headers and the body of a request from Azure API Management to Azure Event Hub
<policies>
<inbound>
<log-to-eventhub logger-id="svenmalvik-logger">
@{
var body = context.Request.Body.As<string>(preserveContent: true);
// ToBase64 to get it better tranfered
var bodyToLog = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(body));
var headers = context.Request.Headers;
Dictionary<string, string> contextProperties = new Dictionary<string, string>();
foreach (var h in headers) {
contextProperties.Add(string.Format("{0}", h.Key), String.Join(", ", h.Value));
}
var requestLogMessage = new {
Headers = contextProperties,
Body = bodyToLog
};
return JsonConvert.SerializeObject(requestLogMessage);
}
</log-to-eventhub>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment