如何使用 log-to-eventhub 策略将带有请求和响应的单个事件发送到事件中心
How to send a single event with request and response to event hubs using log-to-eventhub policy
我正在尝试将来自 API 管理网关的请求和响应记录到 Azure 事件 Hubs.I 正在使用 "log-to-event-hub" 策略 that.I 想要发送包含请求和响应的事件中心的单个事件。
我尝试将事件中心策略与请求和响应一起包含在入站策略中,但我只收到请求而不是 response.Similarly 我尝试将其包含在出站策略中但得到了只有 response.As 我正在将事件中心日志发送到 Azure Log Analytics 我想获得完整的请求和响应 together.I 知道在入站和出站策略中保持 "log-to-event-hub" 策略会给我两个不同的日志事件。
<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>
是否可以在同一个事件中同时记录两个事件,并且只记录一个事件。
我将从变量中的请求中捕获所需的值,将其与出站策略中的请求值组合并记录在其中:
<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>
我正在尝试将来自 API 管理网关的请求和响应记录到 Azure 事件 Hubs.I 正在使用 "log-to-event-hub" 策略 that.I 想要发送包含请求和响应的事件中心的单个事件。
我尝试将事件中心策略与请求和响应一起包含在入站策略中,但我只收到请求而不是 response.Similarly 我尝试将其包含在出站策略中但得到了只有 response.As 我正在将事件中心日志发送到 Azure Log Analytics 我想获得完整的请求和响应 together.I 知道在入站和出站策略中保持 "log-to-event-hub" 策略会给我两个不同的日志事件。
<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>
是否可以在同一个事件中同时记录两个事件,并且只记录一个事件。
我将从变量中的请求中捕获所需的值,将其与出站策略中的请求值组合并记录在其中:
<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>