diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bbd88d0..91fe0a88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and is followi - [#22](https://github.com/FantasticFiasco/serilog-sinks-http/issues/22) Batch formatter `ArrayBatchFormatter` which is compatible with the Logstash HTTP input plugin configured to use the JSON codec +### Fixed + +- Prevent posting HTTP messages without any log events + ## [4.0.0] - 2017-06-17 ### Added diff --git a/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/ArrayBatchFormatter.cs b/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/ArrayBatchFormatter.cs index 82d6b81c..670c435c 100644 --- a/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/ArrayBatchFormatter.cs +++ b/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/ArrayBatchFormatter.cs @@ -15,6 +15,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; namespace Serilog.Sinks.Http.BatchFormatters { @@ -58,6 +59,12 @@ public override void Format(IEnumerable logEvents, TextWriter output) if (output == null) throw new ArgumentNullException(nameof(output)); + // Abort if sequence of log events is empty + if (!logEvents.Any()) + { + return; + } + output.Write("["); var delimStart = string.Empty; diff --git a/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/DefaultBatchFormatter.cs b/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/DefaultBatchFormatter.cs index ce5ee82c..c2edaeab 100644 --- a/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/DefaultBatchFormatter.cs +++ b/src/Serilog.Sinks.Http/Sinks/Http/BatchFormatters/DefaultBatchFormatter.cs @@ -15,6 +15,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; namespace Serilog.Sinks.Http.BatchFormatters { @@ -60,6 +61,12 @@ public override void Format(IEnumerable logEvents, TextWriter output) if (output == null) throw new ArgumentNullException(nameof(output)); + // Abort if sequence of log events is empty + if (!logEvents.Any()) + { + return; + } + output.Write("{\"events\":["); var delimStart = string.Empty;