From d4b3b325d5696a19e43432352094b923323ed7e0 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 7 Jun 2019 12:58:16 +1000 Subject: [PATCH 1/3] Fix exception handling in BackgroundWorkerSink; resolves #44 --- appveyor.yml | 1 - serilog-sinks-async.sln.DotSettings | 2 ++ .../Serilog.Sinks.Async.csproj | 14 ++++++------- .../Sinks/Async/BackgroundWorkerSink.cs | 21 ++++++++++++------- ...erilog.Sinks.Async.PerformanceTests.csproj | 13 +++++++----- .../Serilog.Sinks.Async.Tests.csproj | 11 ++++++---- 6 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 serilog-sinks-async.sln.DotSettings diff --git a/appveyor.yml b/appveyor.yml index 0e93876..c919437 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,6 @@ version: '{build}' skip_tags: true image: Visual Studio 2017 -configuration: Release build_script: - ps: ./Build.ps1 test: off diff --git a/serilog-sinks-async.sln.DotSettings b/serilog-sinks-async.sln.DotSettings new file mode 100644 index 0000000..bfd92fd --- /dev/null +++ b/serilog-sinks-async.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj b/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj index d14886b..6164403 100644 --- a/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj +++ b/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj @@ -2,10 +2,10 @@ Asynchronous sink wrapper for Serilog. - 1.3.1.0 - 1.3.1 + 1.4.0.0 + 1.4.0 Jezz Santos;Serilog Contributors - net45;netstandard1.1 + net45;netstandard1.1;net461;netstandard2.0 true true Serilog.Sinks.Async @@ -15,7 +15,7 @@ true Serilog.Sinks.Async serilog;async - http://serilog.net/images/serilog-sink-nuget.png + https://serilog.net/images/serilog-sink-nuget.png https://serilog.net http://www.apache.org/licenses/LICENSE-2.0 https://github.com/serilog/serilog-sinks-async @@ -23,16 +23,16 @@ True - + true - + - + diff --git a/src/Serilog.Sinks.Async/Sinks/Async/BackgroundWorkerSink.cs b/src/Serilog.Sinks.Async/Sinks/Async/BackgroundWorkerSink.cs index b77f32b..a27f878 100644 --- a/src/Serilog.Sinks.Async/Sinks/Async/BackgroundWorkerSink.cs +++ b/src/Serilog.Sinks.Async/Sinks/Async/BackgroundWorkerSink.cs @@ -10,7 +10,7 @@ namespace Serilog.Sinks.Async { sealed class BackgroundWorkerSink : ILogEventSink, IAsyncLogEventSinkInspector, IDisposable { - readonly ILogEventSink _pipeline; + readonly ILogEventSink _wrappedSink; readonly bool _blockWhenFull; readonly BlockingCollection _queue; readonly Task _worker; @@ -18,10 +18,10 @@ sealed class BackgroundWorkerSink : ILogEventSink, IAsyncLogEventSinkInspector, long _droppedMessages; - public BackgroundWorkerSink(ILogEventSink pipeline, int bufferCapacity, bool blockWhenFull, IAsyncLogEventSinkMonitor monitor = null) + public BackgroundWorkerSink(ILogEventSink wrappedSink, int bufferCapacity, bool blockWhenFull, IAsyncLogEventSinkMonitor monitor = null) { if (bufferCapacity <= 0) throw new ArgumentOutOfRangeException(nameof(bufferCapacity)); - _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline)); + _wrappedSink = wrappedSink ?? throw new ArgumentNullException(nameof(wrappedSink)); _blockWhenFull = blockWhenFull; _queue = new BlockingCollection(bufferCapacity); _worker = Task.Factory.StartNew(Pump, CancellationToken.None, TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); @@ -64,7 +64,7 @@ public void Dispose() // Allow queued events to be flushed _worker.Wait(); - (_pipeline as IDisposable)?.Dispose(); + (_wrappedSink as IDisposable)?.Dispose(); _monitor?.StopMonitoring(this); } @@ -75,12 +75,19 @@ void Pump() { foreach (var next in _queue.GetConsumingEnumerable()) { - _pipeline.Emit(next); + try + { + _wrappedSink.Emit(next); + } + catch (Exception ex) + { + SelfLog.WriteLine("{0} failed to emit event to wrapped sink: {1}", typeof(BackgroundWorkerSink), ex); + } } } - catch (Exception ex) + catch (Exception fatal) { - SelfLog.WriteLine("{0} fatal error in worker thread: {1}", typeof(BackgroundWorkerSink), ex); + SelfLog.WriteLine("{0} fatal error in worker thread: {1}", typeof(BackgroundWorkerSink), fatal); } } diff --git a/test/Serilog.Sinks.Async.PerformanceTests/Serilog.Sinks.Async.PerformanceTests.csproj b/test/Serilog.Sinks.Async.PerformanceTests/Serilog.Sinks.Async.PerformanceTests.csproj index 54ec36f..ffe0d1d 100644 --- a/test/Serilog.Sinks.Async.PerformanceTests/Serilog.Sinks.Async.PerformanceTests.csproj +++ b/test/Serilog.Sinks.Async.PerformanceTests/Serilog.Sinks.Async.PerformanceTests.csproj @@ -1,7 +1,7 @@  - net46;netcoreapp1.1 + net461;netcoreapp2.0 Serilog.Sinks.Async.PerformanceTests ../../assets/Serilog.snk true @@ -17,10 +17,13 @@ - - - - + + + + all + runtime; build; native; contentfiles; analyzers + + diff --git a/test/Serilog.Sinks.Async.Tests/Serilog.Sinks.Async.Tests.csproj b/test/Serilog.Sinks.Async.Tests/Serilog.Sinks.Async.Tests.csproj index 8b7a497..36f324b 100644 --- a/test/Serilog.Sinks.Async.Tests/Serilog.Sinks.Async.Tests.csproj +++ b/test/Serilog.Sinks.Async.Tests/Serilog.Sinks.Async.Tests.csproj @@ -1,7 +1,7 @@  - net452;netcoreapp1.0 + net461;netcoreapp2.0 Serilog.Sinks.Async.Tests ../../assets/Serilog.snk true @@ -14,9 +14,12 @@ - - - + + + all + runtime; build; native; contentfiles; analyzers + + From 91f7b81b906acb6db733977793b219e86c75149f Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 7 Jun 2019 13:04:21 +1000 Subject: [PATCH 2/3] Jump on VS2019; fix BDN upgrade requirements --- appveyor.yml | 2 +- .../LatencyBenchmark.cs | 6 +++--- .../SignallingSink.cs | 8 ++++---- .../ThroughputBenchmark.cs | 10 +++++----- .../BackgroundWorkerSinkIntegrationSpec.cs | 10 +++++----- .../BackgroundWorkerSinkSpec.cs | 4 ++-- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index c919437..9f0e84f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ version: '{build}' skip_tags: true -image: Visual Studio 2017 +image: Visual Studio 2019 build_script: - ps: ./Build.ps1 test: off diff --git a/test/Serilog.Sinks.Async.PerformanceTests/LatencyBenchmark.cs b/test/Serilog.Sinks.Async.PerformanceTests/LatencyBenchmark.cs index 40b6d72..adaacea 100644 --- a/test/Serilog.Sinks.Async.PerformanceTests/LatencyBenchmark.cs +++ b/test/Serilog.Sinks.Async.PerformanceTests/LatencyBenchmark.cs @@ -11,17 +11,17 @@ namespace Serilog.Sinks.Async.PerformanceTests { public class LatencyBenchmark { - private readonly LogEvent _evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, + readonly LogEvent _evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate(new[] {new TextToken("Hello")}), new LogEventProperty[0]); - private Logger _syncLogger, _asyncLogger, _fileLogger, _asyncFileLogger; + Logger _syncLogger, _asyncLogger, _fileLogger, _asyncFileLogger; static LatencyBenchmark() { SelfLog.Enable(new TerminatingTextWriter()); } - [Setup] + [GlobalSetup] public void Reset() { foreach (var logger in new[] { _syncLogger, _asyncLogger, _fileLogger, _asyncFileLogger}) diff --git a/test/Serilog.Sinks.Async.PerformanceTests/SignallingSink.cs b/test/Serilog.Sinks.Async.PerformanceTests/SignallingSink.cs index a58e22b..2882227 100644 --- a/test/Serilog.Sinks.Async.PerformanceTests/SignallingSink.cs +++ b/test/Serilog.Sinks.Async.PerformanceTests/SignallingSink.cs @@ -5,11 +5,11 @@ namespace Serilog.Sinks.Async.PerformanceTests { - internal class SignallingSink : ILogEventSink + class SignallingSink : ILogEventSink { - private readonly int _expectedCount; - private readonly ManualResetEvent _wh; - private int _current; + readonly int _expectedCount; + readonly ManualResetEvent _wh; + int _current; public SignallingSink(int expectedCount) { diff --git a/test/Serilog.Sinks.Async.PerformanceTests/ThroughputBenchmark.cs b/test/Serilog.Sinks.Async.PerformanceTests/ThroughputBenchmark.cs index 5882197..f527e1e 100644 --- a/test/Serilog.Sinks.Async.PerformanceTests/ThroughputBenchmark.cs +++ b/test/Serilog.Sinks.Async.PerformanceTests/ThroughputBenchmark.cs @@ -8,20 +8,20 @@ namespace Serilog.Sinks.Async.PerformanceTests { public class ThroughputBenchmark { - private const int Count = 10000; + const int Count = 10000; - private readonly LogEvent _evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, + readonly LogEvent _evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate(new[] {new TextToken("Hello")}), new LogEventProperty[0]); - private readonly SignallingSink _signal; - private Logger _syncLogger, _asyncLogger; + readonly SignallingSink _signal; + Logger _syncLogger, _asyncLogger; public ThroughputBenchmark() { _signal = new SignallingSink(Count); } - [Setup] + [GlobalSetup] public void Reset() { _syncLogger?.Dispose(); diff --git a/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkIntegrationSpec.cs b/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkIntegrationSpec.cs index 14c0441..23a937e 100644 --- a/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkIntegrationSpec.cs +++ b/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkIntegrationSpec.cs @@ -15,7 +15,7 @@ public class BackgroundWorkerSinkIntegrationSpec /// /// If , then adds a 1sec delay before every fifth element created /// - private static void CreateAudits(ILogger logger, int count, bool withDelay) + static void CreateAudits(ILogger logger, int count, bool withDelay) { var delay = TimeSpan.FromMilliseconds(1000); var sw = new Stopwatch(); @@ -47,7 +47,7 @@ private static void CreateAudits(ILogger logger, int count, bool withDelay) } } - private static List RetrieveEvents(MemorySink sink, int count) + static List RetrieveEvents(MemorySink sink, int count) { Debug.WriteLine("{0:h:mm:ss tt} Retrieving {1} events", DateTime.Now, count); @@ -91,9 +91,9 @@ public GivenBufferQueueAndDelays() public abstract class SinkSpecBase : IDisposable { - private bool _delayCreation; - private Logger _logger; - private MemorySink _memorySink; + bool _delayCreation; + Logger _logger; + MemorySink _memorySink; protected SinkSpecBase(bool useBufferedQueue, bool delayCreation) { diff --git a/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkSpec.cs b/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkSpec.cs index 5b5876d..dff5d5d 100644 --- a/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkSpec.cs +++ b/test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkSpec.cs @@ -230,12 +230,12 @@ public void MonitorParameterAffordsSinkInspectorSuitableForHealthChecking() Assert.Null(monitor.Inspector); } - private BackgroundWorkerSink CreateSinkWithDefaultOptions() + BackgroundWorkerSink CreateSinkWithDefaultOptions() { return new BackgroundWorkerSink(_logger, 10000, false); } - private static LogEvent CreateEvent() + static LogEvent CreateEvent() { return new LogEvent(DateTimeOffset.MaxValue, LogEventLevel.Error, null, new MessageTemplate("amessage", Enumerable.Empty()), From 8a681a650032a8c3fc529c8f4b7da6ec7592bcd1 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Fri, 7 Jun 2019 18:55:29 +1000 Subject: [PATCH 3/3] --- src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj b/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj index 6164403..382912f 100644 --- a/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj +++ b/src/Serilog.Sinks.Async/Serilog.Sinks.Async.csproj @@ -17,7 +17,7 @@ serilog;async https://serilog.net/images/serilog-sink-nuget.png https://serilog.net - http://www.apache.org/licenses/LICENSE-2.0 + Apache-2.0 https://github.com/serilog/serilog-sinks-async git True