コード例 #1
0
        public async Task Should_send_logs_to_udp_syslog_service()
        {
            var syslogFormatter = new Rfc3164Formatter(Facility.Local0, "TestApp");

            var sink = new SyslogUdpSink(this.endpoint, syslogFormatter, this.batchConfig);
            var log  = GetLogger(sink);

            var receiver = new UdpSyslogReceiver();

            receiver.MessageReceived += (_, msg) =>
            {
                this.messagesReceived.Add(msg);
                this.countdown.Signal();
            };

            var receiveTask = receiver.StartReceiving(this.endpoint, this.cts.Token);

            log.Information("This is test message 1");
            log.Warning("This is test message 2");
            log.Error("This is test message 3");

            await this.countdown.WaitAsync(20000, this.cts.Token);

            this.messagesReceived.Count.ShouldBe(3);
            this.messagesReceived.ShouldContain(x => x.StartsWith("<134>"));
            this.messagesReceived.ShouldContain(x => x.StartsWith("<132>"));
            this.messagesReceived.ShouldContain(x => x.StartsWith("<131>"));

            sink.Dispose();
            this.cts.Cancel();
            await receiveTask;
        }
コード例 #2
0
        private async Task Should_send_logs_to_udp_syslog_service(IPEndPoint endpoint)
        {
            var syslogFormatter = new Rfc3164Formatter(Facility.Local0, "TestApp");

            var sink = new SyslogUdpSink(endpoint, syslogFormatter);

            // Start a simple UDP syslog server that will capture all received messaged
            var receiver = new UdpSyslogReceiver();

            receiver.MessageReceived += (_, msg) =>
            {
                this.messagesReceived.Add(msg);
                this.countdown.Signal();
            };

            var receiveTask = receiver.StartReceiving(endpoint, this.cts.Token);

            // Generate and send 3 log events
            var logEvents = Some.LogEvents(3);
            await sink.EmitBatchAsync(logEvents);

            // Wait until the server has received all the messages we sent, or the timeout expires
            await this.countdown.WaitAsync(4000, this.cts.Token);

            // The server should have received all 3 messages sent by the sink
            this.messagesReceived.Count.ShouldBe(logEvents.Length);
            this.messagesReceived.ShouldAllBe(x => logEvents.Any(e => x.EndsWith(e.MessageTemplate.Text)));

            sink.Dispose();
            this.cts.Cancel();
            await receiveTask;
        }
コード例 #3
0
        private async Task SendUdpAsync(IPAddress address)
        {
            // Start a simple UDP syslog server that will capture all received messaged
            var receiver = new UdpSyslogReceiver(this.cts.Token);

            receiver.MessageReceived += (_, msg) =>
            {
                this.messagesReceived.Add(msg);
                this.countdown.Signal();
            };

            var syslogFormatter = new Rfc3164Formatter(Facility.Local0, "TestApp");

            var ipEndPoint = new IPEndPoint(address, receiver.ListeningIPEndPoint.Port);

            var sink = new SyslogUdpSink(ipEndPoint, syslogFormatter);

            // Generate and send 3 log events
            var logEvents = Some.LogEvents(NumberOfEventsToSend);
            await sink.EmitBatchAsync(logEvents);

            // Wait until the server has received all the messages we sent, or the timeout expires
            await this.countdown.WaitAsync(TimeoutInSeconds, this.cts.Token);

            // The server should have received all 3 messages sent by the sink
            this.messagesReceived.Count.ShouldBe(logEvents.Length);
            this.messagesReceived.ShouldAllBe(x => logEvents.Any(e => x.EndsWith(e.MessageTemplate.Text)));

            sink.Dispose();
            this.cts.Cancel();
        }
コード例 #4
0
        public async Task Extension_method_config_with_port()
        {
            var receiver = new UdpSyslogReceiver(this.cts.Token);

            var logger = new LoggerConfiguration().MinimumLevel.Debug();

            logger.WriteTo.UdpSyslog(IPAddress.Loopback.ToString(),
                                     receiver.ListeningIPEndPoint.Port);

            await TestLoggerFromExtensionMethod(logger, receiver);
        }
コード例 #5
0
        public async Task Extension_method_config_with_port_and_Rfc5424_format()
        {
            var receiver = new UdpSyslogReceiver(this.cts.Token);

            var logger = new LoggerConfiguration();

            logger.WriteTo.UdpSyslog(IPAddress.Loopback.ToString(),
                                     receiver.ListeningIPEndPoint.Port,
                                     format: SyslogFormat.RFC5424);

            await TestLoggerFromExtensionMethod(logger, receiver);
        }
コード例 #6
0
        public async Task Extension_method_config_with_port_and_restricted_minimum_level()
        {
            var receiver = new UdpSyslogReceiver(this.cts.Token);

            var logger = new LoggerConfiguration();

            logger.WriteTo.UdpSyslog(IPAddress.Loopback.ToString(),
                                     receiver.ListeningIPEndPoint.Port,
                                     restrictedToMinimumLevel: LogEventLevel.Fatal);

            // In the logger configuration, the restricted minimum level is set to Fatal, which is the
            // highest value. But the log events that will be generated and sent to the log are only at
            // the Information level. Therefore, they will not be written/sent to the log and will be
            // ignored. We don't expect to receive any log events and the test will only complete when
            // the timeout has elapsed.
            await TestLoggerFromExtensionMethod(logger, receiver, 0);
        }
コード例 #7
0
        public async Task Extension_method_config_with_Rfc3164_format_and_severityMapping()
        {
            var receiver = new UdpSyslogReceiver(this.cts.Token);

            var logger = new LoggerConfiguration();

            logger.WriteTo.UdpSyslog(IPAddress.Loopback.ToString(),
                                     receiver.ListeningIPEndPoint.Port,
                                     format: SyslogFormat.RFC3164,
                                     severityMapping: level =>
                                     level switch
            {
                LogEventLevel.Verbose => Severity.Debug,
                LogEventLevel.Debug => Severity.Debug,
                LogEventLevel.Information => Severity.Informational,
                LogEventLevel.Warning => Severity.Warning,
                LogEventLevel.Error => Severity.Error,
                LogEventLevel.Fatal => Severity.Critical,
                _ => throw new ArgumentOutOfRangeException(nameof(level), $"The value {level} is not a valid LogEventLevel.")
            });
コード例 #8
0
        public async Task Extension_method_config_with_port_and_Rfc5424_format_and_messageIdPropertyName()
        {
            var receiver = new UdpSyslogReceiver(this.cts.Token);

            var logger = new LoggerConfiguration();

            logger.WriteTo.UdpSyslog(IPAddress.Loopback.ToString(),
                                     receiver.ListeningIPEndPoint.Port,
                                     format: SyslogFormat.RFC5424,
                                     messageIdPropertyName: "WidgetProcess");

            var evtProperties = new List <LogEventProperty>
            {
                new LogEventProperty("WidgetProcess", new ScalarValue("Widget42")),
            };

            // Should produce log events like:
            // <134>1 2013-12-19T00:01:00.000000-07:00 DSGCH0FP72 testhost.net462.x86 2396 Widget42 [meta WidgetProcess="Widget42"] __2
            var logEvents = Some.LogEvents(NumberOfEventsToSend, evtProperties);

            await TestLoggerFromExtensionMethod(logger, receiver, altLogEvents : logEvents);
        }
コード例 #9
0
        private async Task TestLoggerFromExtensionMethod(LoggerConfiguration logger, UdpSyslogReceiver receiver, int expected = NumberOfEventsToSend, Events.LogEvent[] altLogEvents = null)
        {
            receiver.MessageReceived += (_, msg) =>
            {
                this.messagesReceived.Add(msg);
                this.countdown.Signal();
            };

            var log = logger.CreateLogger();

            var logEvents = altLogEvents ?? Some.LogEvents(NumberOfEventsToSend);

            foreach (var item in logEvents)
            {
                log.Write(item);
            }

            log.Dispose();

            // Wait until the server has received all the messages we sent, or the timeout expires
            await this.countdown.WaitAsync(TimeoutInSeconds, this.cts.Token);

            if (expected > 0)
            {
                // The server should have received all 3 messages sent by the sink
                this.messagesReceived.Count.ShouldBe(NumberOfEventsToSend);
                this.messagesReceived.ShouldAllBe(x => logEvents.Any(e => x.EndsWith(e.MessageTemplate.Text)));
            }

            if (altLogEvents != null)
            {
                var source = altLogEvents.First().Properties.Keys.First();

                this.messagesReceived.ShouldAllBe(x => logEvents.Any(e => x.Contains(source)));
            }

            this.cts.Cancel();
        }