【问题标题】:Azure APIM - how to send trace from within policy expression?Azure APIM - 如何从策略表达式中发送跟踪?
【发布时间】:2021-03-14 13:49:33
【问题描述】:

我在 Azure API 管理中有以下策略。它工作得很好:

<return-response>
<set-status code="302" />
<set-header name="Location" exists-action="override">
    <value>@{
        try
        {
            // ... doing something here that might throw an Exception
            return "http://example.com";
        }
        catch (Exception e)
        {
            // If something failed, it is usually because of an transient error. Then we just send the user to the same URL again to retry.
            return "/";
        }
    }</value>
</set-header>
</return-response>

现在我想在 Catch 块中记录/trace 一条消息。基本上是这样的,但这当然行不通。

catch (Exception e)
{
    <trace source="MyApi" severity="error">
        <message>Error foo</message>
        <metadata name="ErrorMessage" value="@(e.Message)"/>
    </trace>
    return "/";
}

如何执行此操作或如何将日志发送到连接的 Application Insights 实例?

【问题讨论】:

  • 您还对解决方案感兴趣吗?
  • 当然@KaiWalter :)

标签: azure azure-api-management


【解决方案1】:

您让&lt;on-error&gt; 部分处理跟踪:

<policies>
    <inbound>
        <base />
        <return-response>
            <set-status code="302" />
            <set-header name="Location" exists-action="override">
                <value>@{
                        int d = 0;
                        int result = 42 / d;
                        return result.ToString();
                }</value>
            </set-header>
        </return-response>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
        <trace source="ErrorInformation" severity="error">
            <message>ErrorInformation</message>
            <metadata name="EsbCorrelationId" value="@((!context.Variables.ContainsKey("esb-correlation-id") || context.Variables.GetValueOrDefault<string>("esb-correlation-id").Equals(string.Empty)) ? "-none-" : context.Variables.GetValueOrDefault<string>("esb-correlation-id"))" />
            <metadata name="IpAddress" value="@(context.Request.IpAddress.ToString())" />
            <metadata name="LastError.Source" value="@((context.LastError.Source == null || context.LastError.Source.Equals(string.Empty)) ? "-none-" : context.LastError.Source.ToString())" />
            <metadata name="LastError.Reason" value="@((context.LastError.Reason == null || context.LastError.Reason.Equals(string.Empty)) ? "-none-" : context.LastError.Reason.ToString())" />
            <metadata name="LastError.Message" value="@((context.LastError.Message == null || context.LastError.Message.Equals(string.Empty)) ? "-none-" : context.LastError.Message.ToString())" />
            <metadata name="LastError.Scope" value="@((context.LastError.Scope == null || context.LastError.Scope.Equals(string.Empty)) ? "-none-" : context.LastError.Scope.ToString())" />
            <metadata name="LastError.Section" value="@((context.LastError.Section == null || context.LastError.Section.Equals(string.Empty)) ? "-none-" : context.LastError.Section.ToString())" />
            <metadata name="LastError.Path" value="@((context.LastError.Path == null || context.LastError.Path.Equals(string.Empty)) ? "-none-" : context.LastError.Path.ToString())" />
            <metadata name="LastError.PolicyId" value="@((context.LastError.PolicyId == null || context.LastError.PolicyId.Equals(string.Empty)) ? "-none-" : context.LastError.PolicyId.ToString())" />
        </trace>
    </on-error>
</policies>

这将在traces 中为您提供一个条目,并在customDimensions 中提供此内容:

LastError.Message
Expression evaluation failed. Attempted to divide by zero.

LastError.Path
return-response

LastError.PolicyId
-none-

LastError.Reason
ExpressionValueEvaluationFailure

LastError.Scope
operation

LastError.Section
inbound

LastError.Source
set-header

【讨论】:

  • 谢谢,这看起来很整洁!当我找到机会时需要尝试一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 2022-01-02
  • 1970-01-01
  • 2022-08-16
  • 2020-10-29
相关资源
最近更新 更多