gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:57:06 30 4
gpt4 key购买 nike

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com