Exemple #1
0
        public void TestDeserialize()
        {
            string json = "{\"level\":5,\"module\":\"hermes-account\",\"action\":\"find user\",\"message\":\"find user by 15995202790\"}";

            var logMessage = logMessageBusiness.Deserialize(json);

            logMessageBusiness.SetTime(logMessage);
            Console.WriteLine(logMessage.ToString());

            Assert.AreEqual(5, logMessage.Level);
        }
Exemple #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();
                }
            }
        }