
* Specify unix line endings so that `christian-bromann/docusaurus-theme-github-codeblock` formats code blocks correctly * Fix RPC examples
47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using RabbitMQ.Client;
|
|
using RabbitMQ.Client.Events;
|
|
using System.Text;
|
|
|
|
if (args.Length < 1)
|
|
{
|
|
Console.Error.WriteLine("Usage: {0} [info] [warning] [error]",
|
|
Environment.GetCommandLineArgs()[0]);
|
|
Console.WriteLine(" Press [enter] to exit.");
|
|
Console.ReadLine();
|
|
Environment.ExitCode = 1;
|
|
return;
|
|
}
|
|
|
|
var factory = new ConnectionFactory { HostName = "localhost" };
|
|
|
|
using var connection = await factory.CreateConnectionAsync();
|
|
using var channel = await connection.CreateChannelAsync();
|
|
|
|
await channel.ExchangeDeclareAsync(exchange: "direct_logs", type: ExchangeType.Direct);
|
|
|
|
// declare a server-named queue
|
|
var queueDeclareResult = await channel.QueueDeclareAsync();
|
|
string queueName = queueDeclareResult.QueueName;
|
|
|
|
foreach (string? severity in args)
|
|
{
|
|
await channel.QueueBindAsync(queue: queueName, exchange: "direct_logs", routingKey: severity);
|
|
}
|
|
|
|
Console.WriteLine(" [*] Waiting for messages.");
|
|
|
|
var consumer = new AsyncEventingBasicConsumer(channel);
|
|
consumer.ReceivedAsync += (model, ea) =>
|
|
{
|
|
var body = ea.Body.ToArray();
|
|
var message = Encoding.UTF8.GetString(body);
|
|
var routingKey = ea.RoutingKey;
|
|
Console.WriteLine($" [x] Received '{routingKey}':'{message}'");
|
|
return Task.CompletedTask;
|
|
};
|
|
|
|
await channel.BasicConsumeAsync(queueName, autoAck: true, consumer: consumer);
|
|
|
|
Console.WriteLine(" Press [enter] to exit.");
|
|
Console.ReadLine();
|