问题描述

在Application Gateway中,开启WAF(Web application firewall)后,现在需要把访问的日志输出到第三方分析代码中进行分析,如何来获取WAF的诊断日志呢?

整体方案的拓扑图如下:

【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心_连接字符串

本文在实施中将介绍:

1)如何创建Event Hub Namespace(事件中心空间)及Event Hub

2)在Application Gateways(WAF)中配置诊断日志(Diagnostic Logs)

3)(简约)官网中如何消费Event Hub中的数据

 

实施方案

 第一步:创建Event Hub Namespace(事件中心空间)

  • 在Azure门户中进入Event Hub Name 的创建页面:​​Create Namespace - Microsoft Azure 由世纪互联运营​
  • Resource Group可以选择已经存在的任意一个,或者是新建Resource Group,名称为:waf-rg
  • 在Namespace Name中输入:waflogtest01
  • Location 一定要输入与Application Gateway一样的Location。这样在配置诊断日志时才可以自动加载出Event Hub
  • Pricing Tier根据需要选择。这是测试目的,选择Basic层
  • 点击“Review + Create” 按钮,创建资源

【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心_解决方案_02

 

第二步:在Event Hub Namespace中添加Event Hub

进入第一步已创建的Event Hub Namespace页面, 默认Event Hub目录列表为空。点击“Add Event Hub” 按钮。输入Event Hub Name即可

【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心_解决方案_03

 

第三步:在Application Gateway中配置诊断日志(Diagnostic Logs),发送日志到EventHub中

  • 在Application Gateway页面,选择Diagnostic Settings目录
  • 点击“Add diagnostic setting”链接,进入配置页面
  • Diagnostic setting name 设置为 dslogtoeventhub01
  • 勾选上“ApplicationGatewayAccessLog“ “ApplicationGatewayPerformanceLog” ”ApplicationGatewayFirewallLog”
  • 在右侧选择 Stream to an event hub
  • 选择Event Hub Namespace, Event Hub 以及 访问的密钥 event hub policy name

【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心_解决方案_04

 

(附加) 第四步:从 Azure 事件中心接收事件

本部分介绍如何编写一个使用事件处理器从事件中心接收消息的 .NET Core 控制台应用程序。 该事件处理器通过从事件中心管理持久检查点和并行接收操作,来简化从这些事件中心接收事件的过程。 事件处理器与特定的事件中心和使用者组相关联。 它从事件中心内的多个分区接收事件,并将其传递给处理程序委托,以使用提供的代码进行处理。

 

创建 Azure 存储和 Blob 容器

本快速入门使用 Azure 存储作为检查点存储。 按照以下步骤创建 Azure 存储帐户。

  1. ​创建 Azure 存储帐户​
  2. ​创建一个 blob 容器​
  3. ​获取存储帐户的连接字符串​​请记下该连接字符串和容器名称。 稍后要在接收代码中使用这些信息。

为接收器创建项目

  1. 在“解决方案资源管理器”窗口中,右键单击“EventHubQuickStart”解决方案,指向“添加”,然后选择“新建项目”。
  2. 依次选择“控制台应用(.NET Core)”、“下一步”。
  3. 输入 EventHubsReceiver 作为“项目名称”,然后选择“创建”。

添加事件中心 NuGet 包

  1. 在菜单中选择“工具” > “NuGet 包管理器” > “包管理器控制台”。
  2. 运行以下命令安装 Azure.Messaging.EventHubs NuGet 包:
​​Install-Package Azure.Messaging.EventHubs
​​

  1. 运行以下命令安装 Azure.Messaging.EventHubs.Processor NuGet 包:
​​Install-Package Azure.Messaging.EventHubs.Processor
​​

更新 Main 方法

  1. 在 Program.cs 文件顶部添加以下​​using​​ 语句。
​​using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Processor;
​​
  1. 将事件中心连接字符串和事件中心名称的常量添加到​​Program​​ 类。 请将括号中的占位符替换为在创建事件中心时获取的适当值。 请将括号中的占位符替换为创建事件中心和存储帐户时获取的适当值(访问密钥 - 主连接字符串)。 请确保 ​​{Event Hubs namespace connection string}​​ 是命名空间级别的连接字符串,而不是事件中心字符串
​​private const string ehubNamespaceConnectionString = "<EVENT HUBS NAMESPACE - CONNECTION STRING>";
private const string eventHubName = "<EVENT HUB NAME>";
private const string blobStorageConnectionString = "<AZURE STORAGE CONNECTION STRING>";
private const string blobContainerName = "<BLOB CONTAINER NAME>";
​​
  1. 将​​Main​​ 方法替换为以下 ​​async Main​​ 方法。 参阅代码注释了解详细信息 
​​static async Task Main()
{
// Read from the default consumer group: $Default
string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;

// Create a blob container client that the event processor will use
BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);

// Create an event processor client to process events in the event hub
EventProcessorClient processor = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName);

// Register handlers for processing events and handling errors
processor.ProcessEventAsync += ProcessEventHandler;
processor.ProcessErrorAsync += ProcessErrorHandler;

// Start the processing
await processor.StartProcessingAsync();

// Wait for 30 seconds for the events to be processed
await Task.Delay(TimeSpan.FromSeconds(30));

// Stop the processing
await processor.StopProcessingAsync();
}
​​
  1. 现在,将以下事件和错误处理程序方法添加到类中。
​​static async Task ProcessEventHandler(ProcessEventArgs eventArgs)
{
// Write the body of the event to the console window
Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));

// Update checkpoint in the blob storage so that the app receives only new events the next time it's run
await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken);
}

static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs)
{
// Write details about the error to the console window
Console.WriteLine($"\tPartition '{ eventArgs.PartitionId}': an unhandled exception was encountered. Thi

​s was not expected to happen."); Console.WriteLine(eventArgs.Exception.Message); return Task.CompletedTask; } ​

  1. 生成项目并确保没有错误。
    备注:有关包含更详细注释的完整源代码,请参阅​​GitHub 上的此文件​​。
  2. 运行接收器应用程序。
  3. 应会看到一条消息,指出已接收事件。
  4. 【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心_事件处理_05

  5. 这些事件是前面通过运行发送器程序发送到事件中心的三个事件。

 

 

参考资料

事件中心创建:​​https://docs.azure.cn/zh-cn/event-hubs/event-hubs-create​

事件中心概念:​​https://docs.azure.cn/zh-cn/event-hubs/event-hubs-about​

事件中心分区:​​https://docs.azure.cn/zh-cn/event-hubs/event-hubs-features#partitions​

吞吐量单位:​​https://docs.azure.cn/zh-cn/event-hubs/event-hubs-scalability#throughput-units​

接收发送事件:​​https://docs.azure.cn/zh-cn/event-hubs/event-hubs-dotnet-standard-getstarted-send​

 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!