forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiveLogsDirect.cs
44 lines (36 loc) · 1.58 KB
/
ReceiveLogsDirect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
class ReceiveLogsDirect {
public static void Main(string[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.ExchangeDeclare("direct_logs", "direct");
string queue_name = channel.QueueDeclare();
if (args.Length < 1) {
Console.Error.WriteLine("Usage: {0} [info] [warning] [error]",
Environment.GetCommandLineArgs()[0]);
Environment.ExitCode = 1;
return;
}
foreach (string severity in args) {
channel.QueueBind(queue_name, "direct_logs", severity);
}
Console.WriteLine(" [*] Waiting for messages. " +
"To exit press CTRL+C");
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queue_name, true, consumer);
while(true) {
BasicDeliverEventArgs ea =
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
byte[] body = ea.Body;
string message = System.Text.Encoding.UTF8.GetString(body);
string routingKey = ea.RoutingKey;
Console.WriteLine(" [x] Received '{0}':'{1}'",
routingKey, message);
}
}
}
}