适用于逻辑应用和 Power Automate 的 Azure Monitor 日志连接器Azure Monitor Logs connector for Logic Apps and Power Automate

借助 Azure 逻辑应用Power Automate,可以使用数百个操作为多种服务创建自动化工作流。Azure Logic Apps and Power Automate allow you to create automated workflows using hundreds of actions for a variety of services. 使用 Azure Monitor 日志连接器,可以在 Azure Monitor 中生成可从 Log Analytics 工作区或 Application Insights 应用程序检索数据的工作流。The Azure Monitor Logs connector allows you to build workflows that retrieve data from a Log Analytics workspace or an Application Insights application in Azure Monitor. 本文介绍该连接器附带的操作,并演练如何使用这些数据生成工作流。This article describes the actions included with the connector and provides a walkthrough to build a workflow using this data.

例如,可以创建一个逻辑应用,以在 Office 365 的电子邮件通知中使用 Azure Monitor 日志数据,在 Azure DevOps 中创建 Bug,或者发布 Slack 消息。For example, you can create a logic app to use Azure Monitor log data in an email notification from Office 365, create a bug in Azure DevOps, or post a Slack message. 可通过简单计划或从连接的服务中的某些操作(例如收到电子邮件或推文时)触发工作流。You can trigger a workflow by a simple schedule or from some action in a connected service such as when a mail or a tweet is received.

连接器限制Connector limits

Azure Monitor 日志连接器具有以下限制:The Azure Monitor Logs connector has these limits:

  • 最大数据大小:16 MBMax data size: 16 MB
  • 最大查询响应大小 100 MBMax query response size 100 MB
  • 最大记录数:500,000Max number of records: 500,000
  • 最大查询超时 110 秒。Max query timeout 110 second.
  • 图表可视化效果可能在“日志”页中可用,但在连接器中丢失,因为连接器和“日志”页当前使用的图表库不同。Chart visualizations could be available in Logs page and missing in the connector since the connector and Logs page don't use the same charting libraries currently.

根据数据大小和所用的查询,连接器可能会达到其限制而失败。Depending on the size of your data and the query you use, the connector may hit its limits and fail. 调整触发器重复周期,增加运行频率、减少每次查询的数据量,即可解决这种情况。You can work around such cases when adjusting the trigger recurrence to run more frequently and query less data. 你可以使用能聚合数据的查询来返回更少的记录和列。You can use queries that aggregate your data to return less records and columns.

操作Actions

下表描述了 Azure Monitor 日志连接器附带的操作。The following table describes the actions included with the Azure Monitor Logs connector. 可通过这两个操作对 Log Analytics 工作区或 Application Insights 应用程序运行日志查询。Both allow you to run a log query against a Log Analytics workspace or Application Insights application. 两者的差异在于返回数据的方式。The difference is in the way the data is returned.

备注

Azure Monitor 日志连接器取代了 Azure Log Analytics 连接器Azure Application Insights 连接器The Azure Monitor Logs connector replaces the Azure Log Analytics connector and the Azure Application Insights connector. 此连接器的功能与被取代的连接器相同,是针对 Log Analytics 工作区或 Application Insights 应用程序运行查询的首选方法。This connector provides the same functionality as the others and is the preferred method for running a query against a Log Analytics workspace or an Application Insights application.

操作Action 说明Description
运行查询并列出结果Run query and and list results 以自身对象的形式返回每行。Returns each row as its own object. 若要在工作流的剩余阶段单独处理每行,请使用此操作。Use this action when you want to work with each row separately in the rest of the workflow. 该操作通常后接 For each 活动The action is typically followed by a For each activity.
运行查询并将结果可视化Run query and and visualize results 以单个带有格式的对象形式返回结果集中的所有行。Returns all rows in the result set as a single formatted object. 若要在工作流的剩余阶段使用整个结果集(例如,在邮件中发送结果),请使用此操作。Use this action when you want to use the result set together in the rest of the workflow, such as sending the results in a mail.

演练Walkthroughs

以下教程演示了 Azure 逻辑应用中 Azure Monitor 连接器的用法。The following tutorials illustrate the use of the Azure Monitor connectors in Azure Logic Apps. 可以在 Power Automate 中执行相同的示例,唯一的差别是创建初始工作流并在完成创建后运行工作流的方式。You can perform these same example with Power Automate, the only difference being how to you create the initial workflow and run it when complete. 两者的工作流和操作配置相同。Configuration of the workflow and actions is the same between both. 若要开始,请参阅在 Power Automate 中从模板创建流See Create a flow from a template in Power Automate to get started.

创建逻辑应用Create a Logic App

在 Azure 门户中转到“逻辑应用”,然后单击“添加”。 Go to Logic Apps in the Azure portal and click Add. 选择用于存储新逻辑应用的“订阅”、“资源组”和“区域”,并为逻辑应用指定唯一的名称。 Select a Subscription, Resource group, and Region to store the new logic app and then give it a unique name. 你可以打开 Log Analytics 设置,以收集有关运行时数据和事件的信息。You can turn on Log Analytics setting to collect information about runtime data and events. 使用 Azure Monitor 日志连接器不需要此设置。This setting isn't required for using the Azure Monitor Logs connector.

创建逻辑应用

单击“查看 + 创建”,然后选择“创建” 。Click Review + create and then Create. 部署完成后,单击“转到资源”打开“逻辑应用设计器”。 When the deployment is complete, click Go to resource to open the Logic Apps Designer.

为逻辑应用创建触发器Create a trigger for the logic app

在“从通用触发器开始”下,选择“重复”。 Under Start with a common trigger, select Recurrence. 这会创建一个按固定间隔自动运行的逻辑应用。This creates a logic app that automatically runs at a regular interval. 在操作的“频率”框中选择“天”,然后在“间隔”框中输入 1 以每日运行工作流一次。 In the Frequency box of the action, select Day and in the Interval box, enter 1 to run the workflow once per day.

重复操作

演练:邮件可视化结果Walkthrough: Mail visualized results

以下教程介绍了如何创建一个通过电子邮件发送 Azure Monitor 日志查询结果的逻辑应用。The following tutorial shows you how to create a logic app that sends the results of an Azure Monitor log query by email.

添加 Azure Monitor 日志操作Add Azure Monitor Logs action

单击“+ 新建步骤”,添加在完成重复操作后运行的操作。Click + New step to add an action that runs after the recurrence action. 在“选择操作”中键入 azure monitor,然后选择“Azure Monitor 日志”。 Under Choose an action, type azure monitor and then select Azure Monitor Logs.

Azure Monitor 日志操作

单击“Azure Log Analytics - 运行查询并将结果可视化”。Click Azure Log Analytics - Run query and visualize results.

在逻辑应用设计器的一个步骤中添加新操作的屏幕截图。

添加 Azure Monitor 日志操作Add Azure Monitor Logs action

选择 Log Analytics 工作区的“订阅”和“资源组”。 Select the Subscription and Resource Group for your Log Analytics workspace. 为“资源类型”选择“Log Analytics 工作区”,然后在“资源名称”下选择工作区的名称。 Select Log Analytics Workspace for the Resource Type and then select the workspace's name under Resource Name.

将以下日志查询添加到“查询”窗口中。Add the following log query to the Query window.

Event
| where EventLevelName == "Error" 
| where TimeGenerated > ago(1day)
| summarize TotalErrors=count() by Computer
| sort by Computer asc   

为“时间范围”选择“在查询中设置”,为“图表类型”选择“HTML 表”。 Select Set in query for the Time Range and HTML Table for the Chart Type.

名为“运行查询并将结果可视化”的新 Azure Monitor 日志操作的设置的屏幕截图。

与当前连接关联的帐户将发送邮件。The mail will be sent by the account associated with the current connection. 可以单击“更改连接”来指定另一帐户。You can specify another account by clicking on Change connection.

添加电子邮件操作Add email action

依次单击“+ 新建步骤”、“+ 添加操作” 。Click + New step, and then click + Add an action. 在“选择操作”中键入 outlook,然后选择“Office 365 Outlook”。 Under Choose an action, type outlook and then select Office 365 Outlook.

选择 Outlook 连接器

选择“发送电子邮件(V2)”。Select Send an email (V2).

Office 365 Outlook 选择窗口

单击“正文”框中的任意位置打开“动态内容”窗口,其中包含逻辑应用中以前操作的值。 Click anywhere in the Body box to open a Dynamic content window opens with values from the previous actions in the logic app. 选择“查看更多”,然后选择“正文”,其中显示了 Log Analytics 操作中的查询结果。 Select See more and then Body which is the results of the query in the Log Analytics action.

选择正文

在“收件人”窗口中指定收件人电子邮件地址,在“主题”中指定主题 。Specify the email address of a recipient in the To window and a subject for the email in Subject.

邮件操作

保存并测试逻辑应用Save and test your logic app

单击“保存”,然后单击“运行”以执行逻辑应用的测试运行。 Click Save and then Run to perform a test run of the logic app.

“保存”和“运行”

逻辑应用完成时,请检查指定的收件人的邮件。When the logic app completes, check the mail of the recipient that you specified. 应已收到正文类似以下内容的邮件:You should have received a mail with a body similar to the following:

示例电子邮件

后续步骤Next steps