From 4b1c5928a4b5480eb8a1edf608c1116b38012393 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 4 Mar 2024 17:56:35 +1000 Subject: [PATCH] Update to Serilog.Sinks.PeriodicBatching v4.0 and break dependency on deprecated `PeriodicBatchingSink` inheritance API (#579) --- README.md | 2 +- .../Serilog.Sinks.Elasticsearch.Sample.csproj | 2 +- serilog-sinks-elasticsearch.sln | 1 + .../Serilog.Sinks.Elasticsearch.csproj | 27 +++++++++--------- .../Sinks/ElasticSearch/ElasticSearchSink.cs | 28 +++++++++++++++++-- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8a4f42d5..71a52ac7 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This repository contains two nuget packages: `Serilog.Sinks.Elasticsearch` and `Serilog.Formatting.Elasticsearch`. -> __Just a heads up that the .NET team @elastic have created their own new Serilog Sink called Elastic.Serilog.Sinks (Package: https://www.nuget.org/packages/Elastic.Serilog.Sinks#readme-body-tab and documentation: https://www.elastic.co/guide/en/ecs-logging/dotnet/current/serilog-data-shipper.html). Althought this current sink will still work, I advice you to have a look first at the official Elastic implementation as it is better supported and more up to date.__ +> __Just a heads up that the .NET team @elastic have created their own new Serilog Sink called Elastic.Serilog.Sinks (Package: https://www.nuget.org/packages/Elastic.Serilog.Sinks#readme-body-tab and documentation: https://www.elastic.co/guide/en/ecs-logging/dotnet/current/serilog-data-shipper.html). Although this current sink will still work, I advise you to have a look first at the official Elastic implementation as it is better supported and more up to date.__ ## Table of contents diff --git a/sample/Serilog.Sinks.Elasticsearch.Sample/Serilog.Sinks.Elasticsearch.Sample.csproj b/sample/Serilog.Sinks.Elasticsearch.Sample/Serilog.Sinks.Elasticsearch.Sample.csproj index d57ab14c..ded70090 100644 --- a/sample/Serilog.Sinks.Elasticsearch.Sample/Serilog.Sinks.Elasticsearch.Sample.csproj +++ b/sample/Serilog.Sinks.Elasticsearch.Sample/Serilog.Sinks.Elasticsearch.Sample.csproj @@ -11,7 +11,7 @@ - + diff --git a/serilog-sinks-elasticsearch.sln b/serilog-sinks-elasticsearch.sln index f36d10e7..7d5fbcb3 100644 --- a/serilog-sinks-elasticsearch.sln +++ b/serilog-sinks-elasticsearch.sln @@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Files", "Files", "{148431F6 LICENSE = LICENSE nuget.config = nuget.config README.md = README.md + GitVersion.yml = GitVersion.yml EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.Elasticsearch", "src\Serilog.Sinks.Elasticsearch\Serilog.Sinks.Elasticsearch.csproj", "{EEB0D119-687E-444E-BF14-9BDAEC9BA3EF}" diff --git a/src/Serilog.Sinks.Elasticsearch/Serilog.Sinks.Elasticsearch.csproj b/src/Serilog.Sinks.Elasticsearch/Serilog.Sinks.Elasticsearch.csproj index 86f8a111..09763ff0 100644 --- a/src/Serilog.Sinks.Elasticsearch/Serilog.Sinks.Elasticsearch.csproj +++ b/src/Serilog.Sinks.Elasticsearch/Serilog.Sinks.Elasticsearch.csproj @@ -4,9 +4,7 @@ Serilog.Sinks.Elasticsearch Serilog sink for Elasticsearch Copyright © Serilog Contributors 2023 - - - netstandard2.0 + netstandard2.0;net6.0 true latest true @@ -22,6 +20,8 @@ https://github.com/serilog-contrib/serilog-sinks-elasticsearch Apache-2.0 git + Serilog + README.md @@ -29,27 +29,26 @@ $(DefineConstants);DURABLE;THREADING_TIMER + + 1591;1701;1702 + $(DefineConstants);DURABLE;THREADING_TIMER;ASYNC_DISPOSABLE + - - - + + - + - - - - <_Parameter1>Serilog.Sinks.Elasticsearch.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b81894191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066b19485ec - - + - + + diff --git a/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticSearchSink.cs b/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticSearchSink.cs index 35d8f4e7..d36f7400 100644 --- a/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticSearchSink.cs +++ b/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticSearchSink.cs @@ -19,14 +19,20 @@ using System.Runtime.Serialization; using System.Threading.Tasks; using Elasticsearch.Net; +using Serilog.Core; using Serilog.Debugging; using Serilog.Events; using Serilog.Sinks.PeriodicBatching; namespace Serilog.Sinks.Elasticsearch { - public sealed class ElasticsearchSink : PeriodicBatchingSink + public sealed class ElasticsearchSink : ILogEventSink, IDisposable +#if ASYNC_DISPOSABLE + , IAsyncDisposable +#endif { + readonly PeriodicBatchingSink _batchingSink; + public ElasticsearchSink(ElasticsearchSinkOptions options) : this( new BatchedElasticsearchSink(options), @@ -42,10 +48,28 @@ public ElasticsearchSink(ElasticsearchSinkOptions options) } private ElasticsearchSink(IBatchedLogEventSink batchedSink, PeriodicBatchingSinkOptions options) - : base(batchedSink, options) { + _batchingSink = new PeriodicBatchingSink(batchedSink, options); + } + + public void Dispose() + { + _batchingSink.Dispose(); + } + + public void Emit(LogEvent logEvent) + { + _batchingSink.Emit(logEvent); + } + +#if ASYNC_DISPOSABLE + public ValueTask DisposeAsync() + { + return _batchingSink.DisposeAsync(); } +#endif } + /// /// Writes log events as documents to ElasticSearch. ///