Logging all headers and the body of a request from Azure API Management to Azure Event Hub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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