You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,15 +4,10 @@ description: Learn how to log events to Azure Event Hubs in Azure API Management
4
4
services: api-management
5
5
documentationcenter: ''
6
6
author: dlepow
7
-
manager: erikre
8
-
editor: ''
9
7
10
-
ms.assetid: 88f6507d-7460-4eb2-bffd-76025b73f8c4
11
8
ms.service: api-management
12
-
ms.workload: mobile
13
-
ms.tgt_pltfrm: na
14
-
ms.topic: article
15
-
ms.date: 01/29/2018
9
+
ms.topic: how-to
10
+
ms.date: 12/20/2022
16
11
ms.author: danlep
17
12
18
13
---
@@ -21,53 +16,139 @@ Azure Event Hubs is a highly scalable data ingress service that can ingest milli
21
16
22
17
This article describes how to log API Management events using Azure Event Hubs.
23
18
24
-
## Create an Azure Event Hub
19
+
## Prerequisites
25
20
26
-
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](../event-hubs/event-hubs-create.md).
21
+
* An API Management service instance. If you don't have one, see [Create an API Management service instance](get-started-create-service-instance.md).
22
+
* An Azure Event Hubs namespace and event hub. For detailed steps, see [Create an Event Hubs namespace and an event hub using the Azure portal](../event-hubs/event-hubs-create.md).
23
+
> [!NOTE]
24
+
> The Event Hubs resource **can be** in a different subscription or even a different tenant than the API Management resource
25
+
26
+
## Configure access to the event hub
27
+
28
+
To log events to the event hub, you need credentials to enable access from API Management. API Management supports two access mechanisms: an Event Hubs connection string, or an API Management managed identity.
29
+
30
+
### Configure event hub connection string
31
+
32
+
To create an Event Hubs connection string, see [Get an Event Hubs connection string](../event-hubs/event-hubs-get-connection-string.md). You can get a connection string to the namespace or the specific event hub you use for logging from API Management
33
+
34
+
### Configure API Management managed identity
27
35
28
36
> [!NOTE]
29
-
> The Event Hub resource **can be** in a different subscription or even a different tenant than the API Management resource
37
+
> Using an API Management managed identity for logging events to an event hub is supported in API Management REST API version `2022-04-01-preview` or later.
38
+
39
+
1. Enable a system-assigned or user-assigned [managed identity for API Management](api-management-howto-use-managed-service-identity.md) in your API Management instance.
40
+
41
+
* If you enable a user-assigned managed identity, take note of the identity's **Client ID**.
42
+
43
+
1. Assign the identity the **Azure Event Hubs Data Owner** role, scoped to the Event Hubs namespace or to the event hub used for logging. To assign the role, use the [Azure portal](../active-directory/managed-identities-azure-resources/howto-assign-access-portal.md) or other Azure tools.
44
+
30
45
31
46
## Create an API Management logger
32
-
Now that you have an Event Hub, the next step is to configure a [Logger](/rest/api/apimanagement/current-ga/logger) in your API Management service so that it can log events to the Event Hub.
33
-
34
-
API Management loggers are configured using the [API Management REST API](/rest/api/apimanagement/ApiManagementREST/API-Management-REST). For detailed request examples, see [how to create Loggers](/rest/api/apimanagement/current-ga/logger/create-or-update).
35
-
36
-
## Configure log-to-eventhub policies
37
-
38
-
Once your logger is configured in API Management, you can configure your log-to-eventhub policy to log the desired events. The log-to-eventhub policy can be used in either the inbound policy section or the outbound policy section.
39
-
40
-
1. Browse to your APIM instance.
41
-
2. Select the API tab.
42
-
3. Select the API to which you want to add the policy. In this example, we're adding a policy to the **Echo API** in the **Unlimited** product.
43
-
4. Select **All operations**.
44
-
5. On the top of the screen, select the Design tab.
45
-
6. In the Inbound or Outbound processing window, click the triangle (next to the pencil).
46
-
7. Select the Code editor. For more information, see [How to set or edit policies](set-edit-policies.md).
47
-
8. Position your cursor in the `inbound` or `outbound` policy section.
48
-
9. In the window on the right, select **Advanced policies** > **Log to EventHub**. This inserts the `log-to-eventhub` policy statement template.
49
-
50
-
```xml
51
-
<log-to-eventhublogger-id="logger-id">
52
-
@{
53
-
return new JObject(
54
-
new JProperty("EventTime", DateTime.UtcNow.ToString()),
55
-
new JProperty("ServiceName", context.Deployment.ServiceName),
56
-
new JProperty("RequestId", context.RequestId),
57
-
new JProperty("RequestIp", context.Request.IpAddress),
58
-
new JProperty("OperationName", context.Operation.Name)
59
-
).ToString();
47
+
Now that you have an event hub, the next step is to configure a [Logger](/rest/api/apimanagement/current-ga/logger) in your API Management service so that it can log events to the event hub.
48
+
49
+
API Management loggers can be configured using the [API Management REST API](/rest/api/apimanagement/current-ga/logger/create-or-update) directly or tools including [Azure PowerShell](/powershell/module/az.apimanagement/new-azapimanagementlogger), a Bicep template, or an Azure Resource Management template.
50
+
51
+
### Logger with connection string credentials
52
+
53
+
#### [PowerShell](#tab/PowerShell)
54
+
55
+
The following example uses the [New-AzApiManagementLogger](/powershell/module/az.apimanagement/new-azapimanagementlogger) cmdlet to create a logger to an event hub by specifying a connection string.
"description": "Event hub logger with connection string",
106
+
"loggerType": "azureEventHub",
107
+
"resourceId": "string"
108
+
}
109
+
}
62
110
```
63
-
Replace `logger-id` with the value you used for `{loggerId}` in the request URL to create the logger in the previous step.
111
+
---
112
+
113
+
### Logger with system-assigned managed identity credentials
114
+
115
+
116
+
### Logger with user-assigned managed identity credentials
117
+
118
+
119
+
## Configure log-to-eventhub policy
120
+
121
+
Once your logger is configured in API Management, you can configure your [log-to-eventhub](api-management-advanced-policies.md#log-to-event-hub) policy to log the desired events. The `log-to-eventhub` policy can be used in either the inbound policy section or the outbound policy section.
122
+
123
+
1. Browse to your API Management instance.
124
+
1. Select **APIs**, and then select the API to which you want to add the policy. In this example, we're adding a policy to the **Echo API** in the **Unlimited** product.
125
+
1. Select **All operations**.
126
+
1. On the top of the screen, select the **Design** tab.
127
+
1. In the Inbound processing or Outbound processing window, select the `</>` (code editor) icon. For more information, see [How to set or edit policies](set-edit-policies.md).
128
+
1. Position your cursor in the `inbound` or `outbound` policy section.
129
+
1. In the window on the right, select **Advanced policies** > **Log to EventHub**. This inserts the `log-to-eventhub` policy statement template.
130
+
131
+
```xml
132
+
<log-to-eventhublogger-id="logger-id">
133
+
@{
134
+
return new JObject(
135
+
new JProperty("EventTime", DateTime.UtcNow.ToString()),
136
+
new JProperty("ServiceName", context.Deployment.ServiceName),
137
+
new JProperty("RequestId", context.RequestId),
138
+
new JProperty("RequestIp", context.Request.IpAddress),
139
+
new JProperty("OperationName", context.Operation.Name)
140
+
).ToString();
141
+
}
142
+
</log-to-eventhub>
143
+
```
64
144
65
-
You can use any expression that returns a string as the value for the `log-to-eventhub` element. 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.
145
+
1. Replace `logger-id` with the name of the logger that you created in the previous step.
146
+
1. You can use any expression that returns a string as the value for the `log-to-eventhub` element. 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.
66
147
67
-
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.
148
+
1. Select **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.
68
149
69
150
> [!NOTE]
70
-
> The maximum supported message size that can be sent to an event hub from this API Management policy is 200 kilobytes (KB). If a message that is sent to an event hub is larger than 200 KB, it will be automatically truncated, and the truncated message will be transferred to event hubs.
151
+
> The maximum supported message size that can be sent to an event hub from this API Management policy is 200 kilobytes (KB). If a message that is sent to an event hub is larger than 200 KB, it will be automatically truncated, and the truncated message will be transferred to the event hub.
71
152
72
153
## Preview the log in Event Hubs by using Azure Stream Analytics
0 commit comments