Send errors to Stackify for logging

This article shows an Azure API management policy sample that demonstrates how to add an error logging policy to send errors to Stackify for logging. To set or edit a policy code, follow the steps described in Set or edit a policy. To see other examples, see policy samples.


Paste the code into the on-error block.

<!-- The policy defined in this file shows how to add an error logging policy to send errors to Stackify for logging. -->
<!-- You must specify the following named values: -->
  <!-- your-stackify-api-key  -->
  <!-- stackify-api-key  - Get this from your Stackify account -->
  <!-- environment-name - This will be send to Stackify for the environment filter, (Production, Dev, Test, etc) -->
  <!-- app-name - The Application name that will be sent to Stackify -->

<!-- Copy the following snippet into the on-error section. -->

    <base />
    <base />
    <base />
    <trace source="OnError">
      On Error
    <send-one-way-request mode="new">
      <set-header name="Content-Type" exists-action="override">
      <set-header name="X-Stackify-PV" exists-action="override">
      <set-header name="X-Stackify-Key" exists-action="override">
        return new JObject(
        new JProperty("Environment","{{environment-name}}"),
        new JProperty("ServerName", context.Deployment.ServiceName),
        new JProperty("AppName", "{{app-name}}"),
        new JProperty("AppLoc", "/usr/local/stackify/stackify-agent"),
        new JProperty("Logger", "stackify-log-log4j12-1.0.12"),
        new JProperty("Platform", "java"),
        new JProperty("Msgs",
        new JArray(
        new JObject(
        new JProperty("Msg", context.LastError.Message),
        new JProperty("Th", "main"),
        new JProperty("EpochMs", (new DateTimeOffset(DateTime.Now)).ToUnixTimeSeconds() * 1000 ),
        new JProperty("Level", "error"),
        new JProperty("SrcMethod", context.LastError.Source)

Next steps

Learn more about APIM policies: