From 80b91cd4f952b55b4c384360c53d498a3f6ccc1f Mon Sep 17 00:00:00 2001 From: Matthew Erbs Date: Sun, 7 Nov 2021 11:15:25 +1000 Subject: [PATCH] 3.7.0 Release (#150) * Bumps to System.Net.Http * add default batch config * Update expired GitHub Key * fix bug dispose (#153) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Mohsen Rajabi --- appveyor.yml | 8 +- build.sh | 6 +- sample/Sample/Sample.csproj | 11 ++- .../Serilog.Sinks.Splunk.csproj | 32 ++----- .../Serilog.Sinks.Splunk.TCP.csproj | 28 ++---- .../Serilog.Sinks.Splunk.UDP.csproj | 25 ++---- .../Splunk/SplunkUdpSinkConnectionInfo.cs | 30 +++++++ src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs | 88 ++++++++++--------- .../Serilog.Sinks.Splunk.Tests.csproj | 15 ++-- 9 files changed, 114 insertions(+), 129 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index c622dd1..f9926d6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,8 +1,8 @@ version: '{build}' skip_tags: true image: - - Visual Studio 2017 - - Ubuntu + - Visual Studio 2019 + - Ubuntu2004 configuration: - Release build_script: @@ -14,13 +14,13 @@ for: - matrix: only: - - image: Ubuntu + - image: Ubuntu2004 build_script: - sh build.sh deploy: - provider: NuGet api_key: - secure: K3/810hkTO6rd2AEHVkUQAadjGmDREus9k96QHu6hxrA1/wRTuAJemHMKtVVgIvf + secure: gjQNjT9fJBA2Bx+TCFnO0SrgjMDe3Wa99Dj0M8ADwAXeT8LIU4ECqjlzvYsapCYK skip_symbols: true on: branch: /^(master|dev)$/ diff --git a/build.sh b/build.sh index 215df03..450515a 100755 --- a/build.sh +++ b/build.sh @@ -5,12 +5,12 @@ dotnet restore # Until # 65 is addressed build only core package. When available move to netstandard for all packages for path in src/**/Serilog.Sinks.Splunk.csproj; do - dotnet build -f netstandard1.1 -c Release ${path} dotnet build -f netstandard2.0 -c Release ${path} + dotnet build -f netstandard2.1 -c Release ${path} done for path in test/*.Tests/*.csproj; do - dotnet test -f netcoreapp2.0 -c Release ${path} + dotnet test -f net5.0 -c Release ${path} done -dotnet build -f netcoreapp2.0 -c Release sample/Sample/Sample.csproj \ No newline at end of file +dotnet build -f net5.0 -c Release sample/Sample/Sample.csproj \ No newline at end of file diff --git a/sample/Sample/Sample.csproj b/sample/Sample/Sample.csproj index e908df8..d4e39be 100644 --- a/sample/Sample/Sample.csproj +++ b/sample/Sample/Sample.csproj @@ -2,15 +2,14 @@ Exe - netcoreapp2.0 + net5.0 - - - - - + + + + diff --git a/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj b/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj index 2e08dcf..7d69174 100644 --- a/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj +++ b/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj @@ -2,16 +2,16 @@ The Splunk Sink for Serilog - 3.6.0 + 3.7.0 Matthew Erbs, Serilog Contributors - net45;netstandard1.1;netstandard2.0 + netstandard2.1;netstandard2.0 true Serilog.Sinks.Splunk Serilog.Sinks.Splunk serilog;splunk;logging;event;collector;hec - http://serilog.net/images/serilog-sink-nuget.png + http://serilog.net/images/serilog-sink-nuget.png https://github.com/serilog/serilog-sinks-splunk - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 https://github.com/serilog/serilog-sinks-splunk git ../../assets/Serilog.snk @@ -20,29 +20,9 @@ true - - - true - - - - - true - - - - - - - - - - - - - - + + diff --git a/src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj b/src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj index d640c76..d41cd8a 100644 --- a/src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj +++ b/src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj @@ -2,16 +2,16 @@ The Splunk TCP Sink for Serilog - 1.4.0 + 1.5.0 Matthew Erbs, Serilog Contributors - net45;netstandard2.0 + netstandard2.1;netstandard2.0 true Serilog.Sinks.Splunk.TCP Serilog.Sinks.Splunk.TCP serilog;splunk;logging;tcp - http://serilog.net/images/serilog-sink-nuget.png + http://serilog.net/images/serilog-sink-nuget.png https://github.com/serilog/serilog-sinks-splunk - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 https://github.com/serilog/serilog-sinks-splunk git ../../assets/Serilog.snk @@ -19,24 +19,8 @@ true - - - true - - - - - - - - - - - - - - - + + diff --git a/src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj b/src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj index b9ceef3..95daf56 100644 --- a/src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj +++ b/src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj @@ -2,16 +2,16 @@ The Splunk UDP Sink for Serilog - 1.4.0 + 1.5.0 Matthew Erbs, Serilog Contributors - net45;netstandard2.0 + netstandard2.1;netstandard2.0 true Serilog.Sinks.Splunk.UDP Serilog.Sinks.Splunk.UDP serilog;splunk;logging;udp - http://serilog.net/images/serilog-sink-nuget.png + http://serilog.net/images/serilog-sink-nuget.png https://github.com/serilog/serilog-sinks-splunk - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 https://github.com/serilog/serilog-sinks-splunk git ../../assets/Serilog.snk @@ -19,22 +19,7 @@ true - - - true - - - - - - - - - - - - - + diff --git a/src/Serilog.Sinks.UDP/Sinks/Splunk/SplunkUdpSinkConnectionInfo.cs b/src/Serilog.Sinks.UDP/Sinks/Splunk/SplunkUdpSinkConnectionInfo.cs index 3184826..7c78623 100644 --- a/src/Serilog.Sinks.UDP/Sinks/Splunk/SplunkUdpSinkConnectionInfo.cs +++ b/src/Serilog.Sinks.UDP/Sinks/Splunk/SplunkUdpSinkConnectionInfo.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using System.Net; namespace Serilog.Sinks.Splunk @@ -22,6 +23,8 @@ namespace Serilog.Sinks.Splunk /// public class SplunkUdpSinkConnectionInfo { + private int _queueSizeLimit; + /// /// Splunk host. /// @@ -32,6 +35,33 @@ public class SplunkUdpSinkConnectionInfo /// public int Port { get; } + /// + /// The maximum number of events to post in a single batch. Defaults to: 100. + /// + public int BatchPostingLimit { get; set; } = 100; + + + /// + /// The time to wait between checking for event batches. Defaults to 10 seconds. + /// + public TimeSpan Period { get; set; } = TimeSpan.FromSeconds(10); + + + /// + /// The maximum number of events that will be held in-memory while waiting to ship them to + /// Splunk. Beyond this limit, events will be dropped. The default is 100,000. Has no effect on + /// durable log shipping. + /// + public int QueueSizeLimit + { + get { return _queueSizeLimit; } + set + { + if (value < 0) + throw new ArgumentOutOfRangeException(nameof(QueueSizeLimit), "Queue size limit must be non-zero."); + _queueSizeLimit = value; + } + } /// /// Creates an instance of used /// for defining connection info for connecting using UDP against Splunk. diff --git a/src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs b/src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs index 850581b..9001c1b 100644 --- a/src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs +++ b/src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs @@ -16,21 +16,23 @@ using Serilog.Events; using Serilog.Formatting; using System; +using System.Collections.Generic; using System.IO; using System.Net.Sockets; using System.Text; +using System.Threading.Tasks; +using Serilog.Sinks.PeriodicBatching; namespace Serilog.Sinks.Splunk { /// /// A sink that logs to Splunk over UDP /// - public class UdpSink : ILogEventSink, IDisposable + public class UdpSink : PeriodicBatchingSink { private readonly SplunkUdpSinkConnectionInfo _connectionInfo; private readonly ITextFormatter _formatter; private Socket _socket; - private bool disposedValue = false; /// /// Creates an instance of the Splunk UDP Sink. @@ -48,51 +50,15 @@ public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, IFormatProvider forma /// /// Connection info used for connecting against Splunk. /// Custom formatter to use if you e.g. do not want to use the JsonFormatter. - public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, ITextFormatter formatter) + public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, ITextFormatter formatter) + : base(connectionInfo.BatchPostingLimit, connectionInfo.Period, connectionInfo.QueueSizeLimit) { _connectionInfo = connectionInfo; _formatter = formatter; Connect(); } - - /// - protected virtual void Dispose(bool disposing) - { - if (!disposedValue) - { - if (disposing) - { - DisposeSocket(); - } - - disposedValue = true; - } - } - - /// - public void Dispose() - { - Dispose(true); - } - - /// - public void Emit(LogEvent logEvent) - { - byte[] data = Convert(logEvent); - - try - { - _socket.Send(data); - } - catch (SocketException) - { - // Try to reconnect and log - DisposeSocket(); - Connect(); - _socket.Send(data); - } - } - + + private byte[] Convert(LogEvent logEvent) { var sb = new StringBuilder(); @@ -107,6 +73,14 @@ private void Connect() _socket.Connect(_connectionInfo.Host, _connectionInfo.Port); } + + protected override void Dispose(bool disposing) + { + DisposeSocket(); + + base.Dispose(true); + } + private void DisposeSocket() { _socket?.Close(); @@ -114,6 +88,36 @@ private void DisposeSocket() _socket = null; } + /// + /// Emit a batch of log events, running to completion synchronously. + /// + /// The events to emit. + /// + /// Override either + /// or , + /// not both. + /// + protected override async Task EmitBatchAsync(IEnumerable events) + { + foreach (var logEvent in events) + { + byte[] data = Convert(logEvent); + + try + { + _socket.Send(data); + } + catch (SocketException) + { + // Try to reconnect and log + DisposeSocket(); + Connect(); + _socket.Send(data); + } + } + } + + private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate) { return new SplunkJsonFormatter(renderTemplate, formatProvider); diff --git a/test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj b/test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj index be7fb1b..0f38bad 100644 --- a/test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj +++ b/test/Serilog.Sinks.Splunk.Tests/Serilog.Sinks.Splunk.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.0 + net5.0 Serilog.Sinks.Splunk.Tests Serilog.Sinks.Splunk.Tests true @@ -15,11 +15,14 @@ - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +