From 4c74a1675337876e840c9a712c2a809c185dd880 Mon Sep 17 00:00:00 2001 From: Zara28 Date: Mon, 23 Sep 2024 22:45:22 +0400 Subject: [PATCH] =?UTF-8?q?=D1=82=D1=83=D1=82=D0=BE=D1=80=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ dozorova_alena_lab_4/ReceiveLogs/Program.cs | 33 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 628dfee..9759e02 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ /dozorova_alena_lab_4/Worker/.vs /dozorova_alena_lab_4/Worker/bin /dozorova_alena_lab_4/Worker/obj +/dozorova_alena_lab_4/EmitLog/bin +/dozorova_alena_lab_4/ReceiveLogs/.vs +/dozorova_alena_lab_4/ReceiveLogs/bin diff --git a/dozorova_alena_lab_4/ReceiveLogs/Program.cs b/dozorova_alena_lab_4/ReceiveLogs/Program.cs index 3751555..f1a8612 100644 --- a/dozorova_alena_lab_4/ReceiveLogs/Program.cs +++ b/dozorova_alena_lab_4/ReceiveLogs/Program.cs @@ -1,2 +1,31 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using System.Text; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; + +var factory = new ConnectionFactory { HostName = "localhost" }; +using var connection = factory.CreateConnection(); +using var channel = connection.CreateModel(); + +channel.ExchangeDeclare(exchange: "logs", type: ExchangeType.Fanout); + +// declare a server-named queue +var queueName = channel.QueueDeclare().QueueName; +channel.QueueBind(queue: queueName, + exchange: "logs", + routingKey: string.Empty); + +Console.WriteLine(" [*] Waiting for logs."); + +var consumer = new EventingBasicConsumer(channel); +consumer.Received += (model, ea) => +{ + byte[] body = ea.Body.ToArray(); + var message = Encoding.UTF8.GetString(body); + Console.WriteLine($" [x] {message}"); +}; +channel.BasicConsume(queue: queueName, + autoAck: true, + consumer: consumer); + +Console.WriteLine(" Press [enter] to exit."); +Console.ReadLine(); \ No newline at end of file