Sandeep Thomas
Sandeep Thomas

Reputation: 4759

Azure APIM trace in policies showing the variable name, not the value

I am facing a trouble with using trace in Azure APIM policies.

For example below trace message

                <set-variable name="expiresOn" value="@(Uri.EscapeDataString(DateTime.UtcNow.AddDays(30).ToString("yyyy-MM-ddTHH:mm:ssZ")))" />
            <trace source="policy" severity="information">Generated expiresOn value: @(context.Variables["expiresOn"])</trace>
            <!-- Assign full URL to a variable and log it -->
            <set-variable name="tokenRequestUrl" value="@($"https://myapp.com/rest/jwt/?expiresOn={context.Variables["expiresOn"]}")" />
            <trace source="policy" severity="information">Token request URL: @(context.Variables.GetValueOrDefault("tokenRequestUrl", "NULL"))</trace>

these are all only showing this in app insight

enter image description here

Why they are not printing the values?? or the values are not really assigning??

what I did wrong here

Upvotes: 0

Views: 23

Answers (2)

Vitaliy Kurokhtin
Vitaliy Kurokhtin

Reputation: 7810

APIM policy expressions (https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions) can't be used in the middle of attribute or element value. The whole value should be either an expression (begins with @( or ${) or it is treated as a static text.

In your case try:

<trace source="policy" severity="information">@("Generated expiresOn value: " + (string)context.Variables["expiresOn"])</trace>

Upvotes: 0

10p
10p

Reputation: 6778

Try using the multi-statement syntax inside trace, for example:

@{return context.Variables.GetValueOrDefault<string>("tokenRequestUrl", "NULL");}

P.S. You seem to be missing the <message> node inside trace.

Upvotes: 1

Related Questions