【Azure 事件中心】為應用程序網關(Application Gateway with WAF) 配置診斷日志,發送到事件中心


問題描述

在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 存儲帳戶。

  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
    
  3. 運行以下命令安裝 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; 
  2. 將事件中心連接字符串和事件中心名稱的常量添加到 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>"; 
  3. 將 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(); } 
  4. 現在,將以下事件和錯誤處理程序方法添加到類中。

        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; } 
  5. 生成項目並確保沒有錯誤。

     備注:有關包含更詳細注釋的完整源代碼,請參閱 GitHub 上的此文件

  6. 運行接收器應用程序。

  7. 應會看到一條消息,指出已接收事件。

    已接收事件

    這些事件是前面通過運行發送器程序發送到事件中心的三個事件。

 

 

參考資料

事件中心創建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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM