问题描述

在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”链接,进入配置页面
  • Diagnostic setting name 设置为 dslogtoeventhub01 
  • 勾选上“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

【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心的更多相关文章

  1. Azure Application Gateway (5) Application Gateway SSL Offload配置

    <Windows Azure Platform 系列文章目录> 之前有个客户提出了一个需求,他们的互联网访问的架构分为两种: 1.第一层是使用Azure Application Gatew ...

  2. Azure Application Gateway (2) 面向公网的Application Gateway

    <Windows Azure Platform 系列文章目录> 本章将介绍如何创建面向公网的Application Gateway,我们需要准备以下工作: 1.创建新的Azure Reso ...

  3. 【Azure 事件中心】 org.slf4j.Logger 收集 Event Hub SDK(Java) 输出日志并以文件形式保存

    问题描述 在使用Azure Event Hub的SDK时候,常规情况下,发现示例代码中并没有SDK内部的日志输出.因为在Java项目中,没有添加 SLF4J 依赖,已致于在启动时候有如下提示: SLF ...

  4. Azure Application Gateway(一)对后端 Web App 进行负载均衡

    一,引言 今天,我们学习一个新的知识点-----Azure Application Gateway,通过Azure 应用程序网关为我么后端的服务提供负载均衡的功能.我们再文章头中大概先了解一下什么是应 ...

  5. Azure Application Gateway(二)对后端 VM 进行负载均衡

    一,引言 上一节有讲到使用 Azure Application Gateway 为我们后端类型为 Web App 的 Demo 项目提供负载均衡,Azure Application Gateway 的 ...

  6. Azure Application Gateway (4) 设置URL路由 - PowerShell

    <Windows Azure Platform 系列文章目录> 本文将介绍如果使用Azure PowerShell,创建Azure Application Gateway URL Rout ...

  7. Azure Application Gateway (1) 入门

    <Windows Azure Platform 系列文章目录> 请读者注意,Azure Application Gateway在ASM模式下,只能通过PowerShell创建 具体可以参考 ...

  8. 《java小应用程序(Applet)和java应用程序(Application)分别编写的简单计算器》

    Application和Java Applet的区别.Java语言是一种半编译半解释的语言.Java的用户程序分为两类:Java Application和Java Applet.这两类程序在组成结构和 ...

  9. 《Java应用程序(Application)》

    在编写Java应用程序(Application)时可以这样: 1,定义包名. 2, 导入相关的包. 3, 定义一个类. 4,定义相关变量. 5,定义构造函数.(在构造函数内调用init()方法和add ...

随机推荐

  1. 5. VUE 生命周期

    VUE 生命周期 帮助文档: https://cn.vuejs.org/v2/api/#%E9%80%89%E9%A1%B9-%E6%95%B0%E6%8D%AE https://cn.vuejs.o ...

  2. Tomcat部署环境

    环境搭建:程序文件(开发人员),相关的软件(web服务器,应用服务器,数据服务器),硬件(服务器设备上),网络环境. 文档:部署文档说明书(操作系统,硬件配置,服务器软件及相关..) 下面举个列子,考 ...

  3. js收藏展开与隐藏,返回顶部

    var a = document.getElementById("more");var b = document.getElementById("moreList&quo ...

  4. 【MySQL】实现线上千万数据表添加字段操作以及缓存刷新

    需求背景: 由于业务需求,需要在线上用户表添加渠道字段,用于区分不同渠道注册的用户,目前该表有20+个字段,8个索引 线上用户数据大概1500W左右,需要不停机增加数据库字段,同时需要刷新Redis缓 ...

  5. 【swagger】 swagger-ui的升级版swagger-bootstrap-ui

    swagger-bootstrap-ui是基于swagger-ui做了一些优化拓展: swagger-ui的界面: swagger-bootstrap-ui界面: 相比于原生的swagger-ui , ...

  6. 路由器逆向分析------在QEMU MIPS虚拟机上运行MIPS程序(ssh方式)

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/69652258 在QEMU MIPS虚拟机上运行MIPS程序--SSH方式 有关在u ...

  7. Tomcat管理弱口令页面Getshell

    目录 弱口令Getshell 利用Burpsuite对tomcat账号密码进行爆破 弱口令Getshell Tomcat安装完成后会有如下页面,点击该页面的 Manager App 处会弹出输入用户名 ...

  8. <JVM下篇:性能监控与调优篇>补充:浅堆深堆与内存泄露

    笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...

  9. angr脚本——以angrctf解题记录为参考

    angr脚本--以angrctf解题记录为参考 ​ angr是用于逆向工程中进行二进制分析的一个python框架 ​ 符号执行 (Symbolic Execution)是一种程序分析技术.其可以通过分 ...

  10. C# 变体(variance)

    上节讲到了泛型,这节延申一下,讲一下变体. 变体(variance)是协变(convariance)和抗变(也说逆变contravariance)的统称.这个概念在.net 4中引入,在.net 2. ...