示例#1
0
        public void TestInsertMessage()
        {
            LogMessage logMessage = new LogMessage
            {
                Level   = 1,
                Module  = "hermes-log",
                Action  = "test",
                Message = "test insert 2"
            };

            logMessageBusiness.SetTime(logMessage);

            Console.WriteLine(logMessage.Timestamp);

            logMessageBusiness.Insert(logMessage);

            Assert.Pass();
        }
示例#2
0
        /// <summary>
        /// 启动队列监听
        /// </summary>
        public void Run()
        {
            IConnectionFactory factory = new ConnectionFactory()
            {
                HostName = "192.168.1.121",
                Port     = 5672,
                UserName = "******",
                Password = "******"
            };

            LogMessageBusiness logMessageBusiness = new LogMessageBusiness();

            using (var connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "LogQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (sender, ea) =>
                    {
                        // get text
                        var body    = ea.Body.ToArray();
                        var message = Encoding.UTF8.GetString(body);

                        // insert log entity
                        var log = logMessageBusiness.Deserialize(message);
                        logMessageBusiness.SetTime(log);
                        logMessageBusiness.Insert(log);

                        Console.WriteLine(log.ToString());
                    };

                    channel.BasicConsume(queue: "LogQueue", autoAck: true, consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
        }