Example #1
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;
        }
        private async Task SendUnsecureAsync(IPAddress address)
        {
            // Start a simple TCP syslog server that will capture all received messaged
            var receiver = new TcpSyslogReceiver(null, SECURE_PROTOCOLS, this.cts.Token);

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

            this.tcpConfig.Host = address.ToString();
            this.tcpConfig.Port = receiver.IPEndPoint.Port;

            var sink = new SyslogTcpSink(this.tcpConfig);

            // 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(6000, 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();
        }
Example #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();
        }
        public async Task Should_send_logs_to_secure_tcp_syslog_service()
        {
            this.tcpConfig.KeepAlive              = false; // Just to test the negative path
            this.tcpConfig.SecureProtocols        = SECURE_PROTOCOLS;
            this.tcpConfig.CertProvider           = new CertificateProvider(ClientCert);
            this.tcpConfig.CertValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
            {
                // So we know this callback was called
                this.serverCertificate = new X509Certificate2(certificate);
                return(true);
            };

            // Start a simple TCP syslog server that will capture all received messaged
            var receiver = new TcpSyslogReceiver(ServerCert, SECURE_PROTOCOLS, this.cts.Token);

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

            // When a client connects, capture the client certificate they presented
            receiver.ClientAuthenticated += (_, cert) => this.clientCertificate = cert;

            this.tcpConfig.Host = IPAddress.Loopback.ToString();
            this.tcpConfig.Port = receiver.IPEndPoint.Port;

            var sink = new SyslogTcpSink(this.tcpConfig);

            // 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(6000, 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)));

            // The sink should have presented the client certificate to the server
            this.clientCertificate.Thumbprint
            .ShouldBe(ClientCert.Thumbprint, StringCompareShould.IgnoreCase);

            // The sink should have seen the server's certificate in the validation callback
            this.serverCertificate.Thumbprint
            .ShouldBe(ServerCert.Thumbprint, StringCompareShould.IgnoreCase);

            sink.Dispose();
            this.cts.Cancel();
        }
Example #5
0
        public async Task Extension_method_with_batchConfig()
        {
            var receiver = new TcpSyslogReceiver(null, SECURE_PROTOCOLS, this.cts.Token);

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

            // Change the defaults so we can recognize that they are taking effect.
            var batchConfig = new PeriodicBatching.PeriodicBatchingSinkOptions
            {
                EagerlyEmitFirstEvent = false,
                Period = TimeSpan.FromSeconds(TimeoutInSeconds),
            };

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

            logger.WriteTo.TcpSyslog(IPAddress.Loopback.ToString(),
                                     receiver.IPEndPoint.Port,
                                     secureProtocols: SslProtocols.None,
                                     batchConfig: batchConfig);

            var log = logger.CreateLogger();

            // With the batching options set to not eagerly send the first events, we should be able to write
            // some events, wait, check the receiver to make sure we didn't receive any, then wait again.
            var logEvents = Some.LogEvents(NumberOfEventsToSend);

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

            await this.countdown.WaitAsync(TimeSpan.FromSeconds(TimeoutInSeconds / 2), this.cts.Token);

            this.messagesReceived.Count.ShouldBe(0);

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

            this.messagesReceived.Count.ShouldBe(NumberOfEventsToSend);
            this.messagesReceived.ShouldAllBe(x => logEvents.Any(e => x.EndsWith(e.MessageTemplate.Text)));

            log.Dispose();
            this.cts.Cancel();
        }
        public async Task Should_timeout_when_attempting_secure_tcp_to_non_secure_syslog_service()
        {
            // This is all that is needed for the client to attempt to initiate a TLS connection.
            this.tcpConfig.SecureProtocols = SECURE_PROTOCOLS;

            // As for the server, note that we aren't passing in the server certificate and we're
            // instructing it to just listen and read and ignore any data.
            var receiver = new TcpSyslogReceiver(null, SECURE_PROTOCOLS, this.cts.Token, true);

            this.tcpConfig.Host = IPAddress.Loopback.ToString();
            this.tcpConfig.Port = receiver.IPEndPoint.Port;
            this.tcpConfig.TlsAuthenticationTimeout = TimeSpan.FromSeconds(5);

            var sink = new SyslogTcpSink(this.tcpConfig);

            var logEvents = Some.LogEvents(3);

            await Assert.ThrowsAsync <OperationCanceledException>(async() => await sink.EmitBatchAsync(logEvents));

            sink.Dispose();
            this.cts.Cancel();
        }
Example #7
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);
        }
Example #8
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();
        }