Rah*_*war 1 azure azure-api-management
我正在尝试将来自 API 管理网关的请求和响应记录到 Azure 事件中心。我为此使用“日志到事件中心”策略。我想向包含请求的事件中心发送一个事件并一起回应。
我尝试将事件中心策略与请求和响应一起包含在入站策略中,但我只收到请求而不是响应。同样我尝试将其包含在出站策略中,但只得到响应。就像我一样我将事件中心日志发送到 Azure Log Analytics 我想一起获得完整的请求和响应。我知道在入站和出站策略中保留“日志到事件中心”策略会给我两个不同的日志事件.
<inbound>
<set-variable name="message-id" value="@(Guid.NewGuid())" />
<log-to-eventhub logger-id="all-logs" partition-id="0">@{
var requestLine = string.Format("{0} {1} HTTP/1.1\r\n",
context.Request.Method,
context.Request.Url.Path + context.Request.Url.QueryString);
var body = "Request " + context.Request.Body?.As<string>(true) + "Response " + context.Response.Body?.As<string>(true);
var headers = context.Request.Headers
.Where(h => h.Key != "Authorization" && h.Key != "Ocp-Apim-Subscription-Key")
.Select(h => string.Format("{0}: {1}", h.Key, String.Join(", ", h.Value)))
.ToArray<string>();
var headerString = (headers.Any()) ? string.Join("\r\n", headers) + "\r\n" : string.Empty;
return "staging: " + context.Response.StatusCode + " " + context.Variables["message-id"] + "\n"
+ requestLine + headerString + "\r\n" + body;
}</log-to-eventhub>
</inbound>
Run Code Online (Sandbox Code Playgroud)
是否可以在同一个事件中同时拥有两个事件并且只记录一个事件。
我会从一个变量中的请求中捕获所需的值,将它与出站策略中的请求值结合起来,并将其记录在那里:
<policies>
<inbound>
<base />
<set-variable name="requestHeaders" value="@(JsonConvert.SerializeObject(context.Request.Headers))" />
<set-variable name="requestBody" value="@(context.Request.Body.As<string>(true))" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<log-to-eventhub logger-id="testlogger">@{
var content = new JObject();
content["reqHeaders"] = context.Variables.GetValueOrDefault<string>("requestHeaders");
content["reqBody"] = context.Variables.GetValueOrDefault<string>("requestBody");
content["resStatus"] = JsonConvert.SerializeObject(context.Response.StatusCode);
content["resBody"] = context.Response.Body.As<string>(true);
return content.ToString();
}</log-to-eventhub>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1125 次 |
| 最近记录: |