示例#1
0
文件: MQTTTests.cs 项目: PTKu/TcOpen
        public void integrate_with_serilog_test_remote_mqtt_broker()
        {
            var topic             = "hojmorhovetvomojhoroduktokradmourukousiahnenatvojuslobodu";
            var publisherOptions  = new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io").Build();
            var subscriberOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io").Build();
            var factory           = new MqttFactory();
            var subscriberClient  = factory.CreateMqttClient();

            subscriberClient.ConnectAsync(subscriberOptions, System.Threading.CancellationToken.None).Wait();

            subscriberClient.SubscribeAsync(new MQTTnet.Client.Subscribing.MqttClientSubscribeOptions()
            {
                TopicFilters = new List <MqttTopicFilter>()
                {
                    new MqttTopicFilter()
                    {
                        Topic = topic
                    }
                }
            },
                                            System.Threading.CancellationToken.None).Wait();


            MqttApplicationMessage appMessage = null;

            subscriberClient.UseApplicationMessageReceivedHandler((m) => appMessage = m.ApplicationMessage);

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.MQTT(publisherOptions, topic)
                         .CreateLogger();

            Log.Fatal("this is fatal");

            while (appMessage == null)
            {
                System.Threading.Thread.Sleep(10);
            }

            var payload = LogMqttMessagePayload.GetPayload(appMessage.ConvertPayloadToString());

            Assert.AreEqual("this is fatal", payload.MessageTemplate);
            Assert.AreEqual("Fatal", payload.Level);
        }
示例#2
0
文件: MQTTTests.cs 项目: PTKu/TcOpen
        public void emit_test()
        {
            var topic    = "this is my topic";
            var options  = new MqttClientOptionsBuilder().WithTcpServer("localhost").Build();
            var sink     = new MQTTSink(options, topic);
            var logEvent = new Events.LogEvent(System.DateTimeOffset.Now, Events.LogEventLevel.Error, null,
                                               new Events.MessageTemplate("@{payload}", new Parsing.MessageTemplateToken[] { }), new Events.LogEventProperty[] { });

            sink.Emit(logEvent);

            System.Threading.Thread.Sleep(100);

            var actual = messageHandler.ApplicationMessages.Where(p => p.Topic == topic);

            Assert.AreEqual(1, actual.Count());;

            var payload = LogMqttMessagePayload.GetPayload(actual.FirstOrDefault().ConvertPayloadToString());

            Assert.AreEqual("@{payload}", payload.MessageTemplate);
            Assert.AreEqual("Error", payload.Level);
        }
示例#3
0
文件: MQTTTests.cs 项目: PTKu/TcOpen
        public void integrate_with_serilog_test()
        {
            var topic   = "this is my fatal topic";
            var options = new MqttClientOptionsBuilder().WithTcpServer("localhost").Build();

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.MQTT(options, topic)
                         .CreateLogger();

            Log.Fatal("this is fatal");

            System.Threading.Thread.Sleep(100);

            var actual = messageHandler.ApplicationMessages.Where(p => p.Topic == topic);

            Assert.AreEqual(1, actual.Count());

            var payload = LogMqttMessagePayload.GetPayload(actual.FirstOrDefault().ConvertPayloadToString());

            Assert.AreEqual("this is fatal", payload.MessageTemplate);
            Assert.AreEqual("Fatal", payload.Level);
        }