如何在 Azure API 管理中将事件记录到 Azure 事件中心How to log events to Azure Event Hubs in Azure API Management

事件中心是一个高度可缩放的引入服务,每秒可以引入数百万的事件,使用户能够处理和分析连接设备和应用程序生成的海量数据。Azure Event Hubs is a highly scalable data ingress service that can ingest millions of events per second so that you can process and analyze the massive amounts of data produced by your connected devices and applications. 事件中心充当事件管道的“前门”,将数据收集到事件中心后,可以使用任何实时分析提供程序或批处理/存储适配器来转换和存储这些数据。Event Hubs acts as the "front door" for an event pipeline, and once data is collected into an event hub, it can be transformed and stored using any real-time analytics provider or batching/storage adapters. 事件中心可将事件流的生成与这些事件的使用分离开来,因此,事件使用者可以根据自己的计划访问事件。Event Hubs decouples the production of a stream of events from the consumption of those events, so that event consumers can access the events on their own schedule.

本文是将 Azure API 管理与事件中心集成视频的配套内容,介绍了如何使用 Azure 事件中心记录 API 管理事件。This article is a companion to the Integrate Azure API Management with Event Hubs video and describes how to log API Management events using Azure Event Hubs.

创建 Azure 事件中心Create an Azure Event Hub

有关如何创建事件中心以及获取将事件发送到事件中心和从事件中心接收事件所需的连接字符串的详细步骤,请参阅使用 Azure 门户创建事件中心命名空间和事件中心For detailed steps on how to create an event hub and get connection strings that you need to send and receive events to and from the Event Hub, see Create an Event Hubs namespace and an event hub using the Azure portal.

创建 API 管理记录器Create an API Management logger

现在有了事件中心,下一步是在 API 管理服务中配置记录器,以便它可以将事件记录到事件中心。Now that you have an Event Hub, the next step is to configure a Logger in your API Management service so that it can log events to the Event Hub.

使用 API 管理 REST API 配置 API 管理记录器。API Management loggers are configured using the API Management REST API. 有关详细的请求示例,请参阅如何创建记录器For detailed request examples, see how to create Loggers.

配置 log-to-eventhub 策略Configure log-to-eventhub policies

在 API 管理中配置了记录器后,就可以配置 log-to-eventhub 策略以记录所需事件。Once your logger is configured in API Management, you can configure your log-to-eventhub policy to log the desired events. 可在入站策略部分或出站策略部分中使用 log-to-eventhub 策略。The log-to-eventhub policy can be used in either the inbound policy section or the outbound policy section.

  1. 浏览到自己的 APIM 实例。Browse to your APIM instance.
  2. 选择“API”选项卡。Select the API tab.
  3. 选择要将策略添加到的 API。Select the API to which you want to add the policy. 在此示例中,我们向 Unlimited 产品中的 Echo API 添加策略。In this example, we're adding a policy to the Echo API in the Unlimited product.
  4. 选择“所有操作”。Select All operations.
  5. 选择屏幕顶部的“设计”选项卡。On the top of the screen, select the Design tab.
  6. 在“入站或出站处理”窗口中,单击三角形(铅笔旁边)。In the Inbound or Outbound processing window, click the triangle (next to the pencil).
  7. 选择“代码编辑器”。Select the Code editor. 有关详细信息,请参阅如何设置或编辑策略For more information, see How to set or edit policies.
  8. 将光标放在 inboundoutbound 策略部分中。Position your cursor in the inbound or outbound policy section.
  9. 在右侧窗口中,选择“高级策略” > “记录到 EventHub”。In the window on the right, select Advanced policies > Log to EventHub. 这会插入 log-to-eventhub 策略语句模板。This inserts the log-to-eventhub policy statement template.
<log-to-eventhub logger-id="logger-id">
    @{
        return new JObject(
            new JProperty("EventTime", DateTime.UtcNow.ToString()),
            new JProperty("ServiceName", context.Deployment.ServiceName),
            new JProperty("RequestId", context.RequestId),
            new JProperty("RequestIp", context.Request.IpAddress),
            new JProperty("OperationName", context.Operation.Name)
        ).ToString();
    }
</log-to-eventhub>

logger-id 替换为请求 URL 中用于 {loggerId} 的值,以创建上一步中的记录器。Replace logger-id with the value you used for {loggerId} in the request URL to create the logger in the previous step.

可使用返回字符串作为 log-to-eventhub 元素值的任何表达式。You can use any expression that returns a string as the value for the log-to-eventhub element. 在此示例中,JSON 格式的字符串包含记录的日期和时间、服务名称、请求 ID、请求 IP 地址和操作名称。In this example, a string in JSON format containing the date and time, service name, request id, request ip address, and operation name is logged.

单击“保存”保存更新后的策略配置。Click Save to save the updated policy configuration. 保存后,策略立即处于活动状态,并且事件记录到指定的事件中心。As soon as it is saved the policy is active and events are logged to the designated Event Hub.

使用 Azure 流分析在事件中心预览日志Preview the log in Event Hubs by using Azure Stream Analytics

可以使用 Azure 流分析查询来预览事件中心中的日志。You can preview the log in Event Hubs by using Azure Stream Analytics queries.

  1. 在 Azure 门户中,浏览到记录器发送事件到的事件中心。In the Azure portal, browse to the event hub that the logger sends events to.
  2. 在“功能”下,选择“处理数据”选项卡。 Under Features, select the Process data tab.
  3. 在“启用来自事件的实时见解”卡片上选择“浏览”。 On the Enable real time insights from events card, select Explore.
  4. 可以在“输入预览”选项卡上预览日志。如果显示的数据不是最新的,请选择“刷新”查看最新事件。You should be able to preview the log on the Input preview tab. If the data shown isn't current, select Refresh to see the latest events.

后续步骤Next steps