Exemple #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;
        }
        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;
        }
Exemple #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();
        }
Exemple #4
0
        public void Should_override_log_host_name()
        {
            var template  = new MessageTemplateParser().Parse("This is a test message");
            var warnEvent = new LogEvent(this.timestamp, LogEventLevel.Warning, null, template, Enumerable.Empty <LogEventProperty>());

            const string hostname       = "NewHostName";
            var          localFormatter = new Rfc3164Formatter(Facility.User, APP_NAME, null, hostname);
            var          formatted      = localFormatter.FormatMessage(warnEvent);

            var match = this.regex.Match(formatted);

            match.Success.ShouldBeTrue();

            match.Groups["host"].Value.ShouldBe(hostname);
        }
        public void Should_send_logs_to_syslog_service()
        {
            var syslogFormatter = new Rfc3164Formatter(Facility.Local0, "TestApp");
            var syslogService   = A.Fake <LocalSyslogService>();
            var sink            = new SyslogLocalSink(syslogFormatter, syslogService);

            var log = GetLogger(sink);

            log.Information("This is a test message");

            sink.Dispose();

            A.CallTo(() => syslogService.Open()).MustHaveHappened();
            A.CallTo(() => syslogService.WriteLog(134, A <string> .That.EndsWith("This is a test message"))).MustHaveHappened();
            A.CallTo(() => syslogService.Close()).MustHaveHappened();
        }
        public void Should_override_severity_with_alternate_severityMapping()
        {
            const string msg = "The mapping of Serilog EventLogLevel.Verbose has been overridden to Syslog Severity.Debug.";

            var syslogFormatter = new Rfc3164Formatter(Facility.Local0, "TestApp",
                                                       severityMapping: level => SyslogLoggerConfigurationExtensions.ValueBasedLogLevelToSeverityMap(level));

            var syslogService = A.Fake <LocalSyslogService>();
            var sink          = new SyslogLocalSink(syslogFormatter, syslogService);

            var log = new LoggerConfiguration()
                      .WriteTo.Sink(sink).MinimumLevel.Verbose()
                      .CreateLogger();

            log.Verbose(msg);

            sink.Dispose();

            // With the custom severity mapping and a Facility of Local0, the calculated priority should be:
            // Local0 * 8 + Severity.Debug = 16 * 8 + 7 = 135
            A.CallTo(() => syslogService.Open()).MustHaveHappened();
            A.CallTo(() => syslogService.WriteLog(135, A <string> .That.EndsWith(msg))).MustHaveHappened();
            A.CallTo(() => syslogService.Close()).MustHaveHappened();
        }