Azure APIM - 如何从策略表达式内发送跟踪?

标签 azure azure-api-management

我在 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>

现在我想登录/trace Catch block 内的消息。基本上是这样的,但这当然行不通。

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

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

最佳答案

你让<on-error>部分处理跟踪:

<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

关于Azure APIM - 如何从策略表达式内发送跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66274733/

相关文章:

python - Azure 上使用 SQLAlchemy 的 MSSQL "connection reset by peer"

azure - 如何从azure apim记录jwt属性?

azure - 如何创建 Powershell 脚本来创建 Azure API 管理资源

Azure API 管理 key

Azure API 应用程序问题 - 身份验证

azure - Service Fabric 中的 session 管理?

azure - 自定义 Azure 虚拟机镜像

c# - 尝试列出从 REST API 过滤的订阅

azure - 如何本地化 Azure API 管理开发人员门户使用的字符串?

azure - 为 VMSS 添加 Azure 诊断设置