天天看點

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

問題描述

在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) 配置診斷日志,發送到事件中心

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

進入第一步已建立的Event Hub Namespace頁面, 預設Event Hub目錄清單為空。點選“Add Event Hub” 按鈕。輸入Event Hub Name即可

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

第三步:在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) 配置診斷日志,發送到事件中心

(附加) 第四步:從 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. 應會看到一條消息,指出已接收事件。
    【Azure 事件中心】為應用程式網關(Application Gateway with WAF) 配置診斷日志,發送到事件中心
    這些事件是前面通過運作發送器程式發送到事件中心的三個事件。

參考資料

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

當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!

繼續閱讀