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; }
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 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(); }
/// <summary> /// Adds a sink that writes log events to a UDP syslog server /// </summary> /// <param name="loggerSinkConfig">The logger configuration</param> /// <param name="host">Hostname of the syslog server</param> /// <param name="port">Port the syslog server is listening on</param> /// <param name="appName">The name of the application. Defaults to the current process name</param> /// <param name="format">The syslog message format to be used</param> /// <param name="facility">The category of the application</param> /// <param name="outputTemplate">A message template describing the output messages /// <seealso cref="https://github.com/serilog/serilog/wiki/Formatting-Output"/> /// </param> public static LoggerConfiguration UdpSyslog(this LoggerSinkConfiguration loggerSinkConfig, string host, int port = 514, string appName = null, SyslogFormat format = SyslogFormat.RFC3164, Facility facility = Facility.Local0, string outputTemplate = null) { if (String.IsNullOrWhiteSpace(host)) { throw new ArgumentException(nameof(host)); } var formatter = GetFormatter(format, appName, facility, outputTemplate); var endpoint = ResolveIP(host, port); var sink = new SyslogUdpSink(endpoint, formatter, BatchConfig.Default); return(loggerSinkConfig.Sink(sink)); }