問題描述
在Application Gateway中,開啟WAF(Web application firewall)后,現在需要把訪問的日志輸出到第三方分析代碼中進行分析,如何來獲取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” 按鈕,創建資源
第二步:在Event Hub Namespace中添加Event Hub
進入第一步已創建的Event Hub Namespace頁面, 默認Event Hub目錄列表為空。點擊“Add Event Hub” 按鈕。輸入Event Hub Name即可
第三步:在Application Gateway中配置診斷日志(Diagnostic Logs),發送日志到EventHub中
- 在Application Gateway頁面,選擇Diagnostic Settings目錄
- 點擊“Add diagnostic setting”鏈接,進入配置頁面
-
- 勾選上“ApplicationGatewayAccessLog“ “ApplicationGatewayPerformanceLog” ”ApplicationGatewayFirewallLog”
- 在右側選擇 Stream to an event hub
- 選擇Event Hub Namespace, Event Hub 以及 訪問的密鑰 event hub policy name
(附加) 第四步:從 Azure 事件中心接收事件
本部分介紹如何編寫一個使用事件處理器從事件中心接收消息的 .NET Core 控制台應用程序。 該事件處理器通過從事件中心管理持久檢查點和並行接收操作,來簡化從這些事件中心接收事件的過程。 事件處理器與特定的事件中心和使用者組相關聯。 它從事件中心內的多個分區接收事件,並將其傳遞給處理程序委托,以使用提供的代碼進行處理。
創建 Azure 存儲和 Blob 容器
本快速入門使用 Azure 存儲作為檢查點存儲。 按照以下步驟創建 Azure 存儲帳戶。
請記下該連接字符串和容器名稱。 稍后要在接收代碼中使用這些信息。
為接收器創建項目
- 在“解決方案資源管理器”窗口中,右鍵單擊“EventHubQuickStart”解決方案,指向“添加”,然后選擇“新建項目”。
- 依次選擇“控制台應用(.NET Core)”、“下一步”。
- 輸入 EventHubsReceiver 作為“項目名稱”,然后選擇“創建”。
添加事件中心 NuGet 包
在菜單中選擇“工具” > “NuGet 包管理器” > “包管理器控制台”。
運行以下命令安裝 Azure.Messaging.EventHubs NuGet 包:
Install-Package Azure.Messaging.EventHubs
運行以下命令安裝 Azure.Messaging.EventHubs.Processor NuGet 包:
Install-Package Azure.Messaging.EventHubs.Processor
更新 Main 方法
在 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;
將事件中心連接字符串和事件中心名稱的常量添加到
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>";
將
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(); }
現在,將以下事件和錯誤處理程序方法添加到類中。
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. This was not expected to happen."); Console.WriteLine(eventArgs.Exception.Message); return Task.CompletedTask; }
生成項目並確保沒有錯誤。
備注:有關包含更詳細注釋的完整源代碼,請參閱 GitHub 上的此文件。
運行接收器應用程序。
應會看到一條消息,指出已接收事件。
這些事件是前面通過運行發送器程序發送到事件中心的三個事件。
參考資料
事件中心創建: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