diff --git a/.dockerignore b/.dockerignore index a02c9ed..cbbd0b5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,2 @@ -bin\ -obj\ \ No newline at end of file +bin/ +obj/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e74cfad..c42262e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ -FROM microsoft/dotnet:2.1-sdk AS build -ADD . / +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +COPY . / WORKDIR /sample/Sample RUN dotnet restore -RUN dotnet publish -c Release -o out -f netcoreapp2.0 +RUN dotnet publish -c Release -o out -f net6.0 -FROM microsoft/dotnet:2.1-runtime AS runtime +FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime WORKDIR /sample/Sample COPY --from=build /sample/Sample/out ./ ENTRYPOINT ["dotnet", "Sample.dll"] \ No newline at end of file diff --git a/build.sh b/build.sh index 450515a..f3c086a 100755 --- a/build.sh +++ b/build.sh @@ -7,10 +7,11 @@ dotnet restore for path in src/**/Serilog.Sinks.Splunk.csproj; do dotnet build -f netstandard2.0 -c Release ${path} dotnet build -f netstandard2.1 -c Release ${path} + dotnet build -f net6.0 -c Release ${path} done for path in test/*.Tests/*.csproj; do - dotnet test -f net5.0 -c Release ${path} + dotnet test -f net6.0 -c Release ${path} done -dotnet build -f net5.0 -c Release sample/Sample/Sample.csproj \ No newline at end of file +dotnet build -f net6.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 3402781..35010d3 100644 --- a/sample/Sample/Sample.csproj +++ b/sample/Sample/Sample.csproj @@ -1,17 +1,15 @@ - + Exe net6.0 - 7e06bf41-4a1d-4a41-bcf3-cacd41388d5f - - - - - + + + + diff --git a/sample/splunk/Dockerfile b/sample/splunk/Dockerfile index 868c0df..9f46425 100644 --- a/sample/splunk/Dockerfile +++ b/sample/splunk/Dockerfile @@ -1,2 +1,2 @@ -FROM splunk/splunk:9.1 +FROM splunk/splunk:latest ADD etc ${SPLUNK_HOME}/etc \ No newline at end of file diff --git a/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj b/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj index de5c091..98a67f0 100644 --- a/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj +++ b/src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj @@ -1,10 +1,10 @@ - + The Splunk Sink for Serilog - 3.7.0 + 4.0.0 Matthew Erbs, Serilog Contributors - netstandard2.1;netstandard2.0 + netstandard2.1;netstandard2.0;net6.0 true Serilog.Sinks.Splunk Serilog.Sinks.Splunk @@ -18,12 +18,13 @@ true true true - latest + Serilog + latest - - + + diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs index 68c8fc4..8e2be66 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs @@ -29,9 +29,9 @@ namespace Serilog.Sinks.Splunk /// /// A sink to log to the Event Collector available in Splunk 6.3 /// - public class EventCollectorSink : PeriodicBatchingSink + public class EventCollectorSink : IBatchedLogEventSink { - private const int DefaultQueueLimit = 100000; + internal const int DefaultQueueLimit = 100000; private readonly string _splunkHost; private readonly string _uriPath; @@ -54,26 +54,17 @@ public class EventCollectorSink : PeriodicBatchingSink /// /// The host of the Splunk instance with the Event collector configured /// The token to use when authenticating with the event collector - /// The size of the batch when sending to the event collector /// The format provider used when rendering the message /// Whether to render the message template - /// The interval in seconds that batching should occur - /// Maximum number of events in the queue public EventCollectorSink( string splunkHost, string eventCollectorToken, - int batchIntervalInSeconds = 5, - int batchSizeLimit = 100, - int? queueLimit = null, IFormatProvider formatProvider = null, bool renderTemplate = true) : this( splunkHost, eventCollectorToken, null, null, null, null, null, - batchIntervalInSeconds, - batchSizeLimit, - queueLimit, formatProvider, renderTemplate) { @@ -85,11 +76,8 @@ public EventCollectorSink( /// The host of the Splunk instance with the Event collector configured /// The token to use when authenticating with the event collector /// Change the default endpoint of the Event Collector e.g. services/collector/event - /// The size of the batch when sending to the event collector /// The format provider used when rendering the message /// Whether to render the message template - /// The interval in seconds that batching should occur - /// Maximum number of events in the queue /// The Splunk index to log to /// The source of the event /// The source type of the event @@ -103,9 +91,6 @@ public EventCollectorSink( string sourceType, string host, string index, - int batchIntervalInSeconds, - int batchSizeLimit, - int? queueLimit, IFormatProvider formatProvider = null, bool renderTemplate = true, HttpMessageHandler messageHandler = null) @@ -113,9 +98,7 @@ public EventCollectorSink( splunkHost, eventCollectorToken, uriPath, - batchIntervalInSeconds, - batchSizeLimit, - queueLimit, + new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index), messageHandler) { @@ -127,11 +110,8 @@ public EventCollectorSink( /// The host of the Splunk instance with the Event collector configured /// The token to use when authenticating with the event collector /// Change the default endpoint of the Event Collector e.g. services/collector/event - /// The size of the batch when sending to the event collector - /// Maximum number of events in the queue /// The format provider used when rendering the message /// Whether to render the message template - /// The interval in seconds that batching should occur /// The Splunk index to log to /// Add extra CustomExtraFields for Splunk to index /// The source of the event @@ -147,9 +127,6 @@ public EventCollectorSink( string host, string index, CustomFields fields, - int batchIntervalInSeconds, - int batchSizeLimit, - int? queueLimit, IFormatProvider formatProvider = null, bool renderTemplate = true, HttpMessageHandler messageHandler = null) @@ -158,9 +135,6 @@ public EventCollectorSink( splunkHost, eventCollectorToken, uriPath, - batchIntervalInSeconds, - batchSizeLimit, - queueLimit, new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index, fields), messageHandler) { @@ -172,21 +146,14 @@ public EventCollectorSink( /// The host of the Splunk instance with the Event collector configured /// The token to use when authenticating with the event collector /// Change the default endpoint of the Event Collector e.g. services/collector/event - /// The size of the batch when sending to the event collector - /// The interval in seconds that batching should occur - /// Maximum number of events in the queue /// The text formatter used to render log events into a JSON format for consumption by Splunk /// The handler used to send HTTP requests public EventCollectorSink( string splunkHost, string eventCollectorToken, string uriPath, - int batchIntervalInSeconds, - int batchSizeLimit, - int? queueLimit, ITextFormatter jsonFormatter, HttpMessageHandler messageHandler = null) - : base(batchSizeLimit, TimeSpan.FromSeconds(batchIntervalInSeconds), queueLimit ?? DefaultQueueLimit) { _uriPath = uriPath; _splunkHost = splunkHost; @@ -197,14 +164,8 @@ public EventCollectorSink( : new EventCollectorClient(eventCollectorToken); } - /// - /// Emit a batch of log events, running asynchronously. - /// - /// The events to emit. - /// - /// Override either or , not both. - /// - protected override async Task EmitBatchAsync(IEnumerable events) + /// + public async Task EmitBatchAsync(IEnumerable events) { var allEvents = new StringWriter(); @@ -233,5 +194,8 @@ protected override async Task EmitBatchAsync(IEnumerable events) } } } + + /// + public Task OnEmptyBatchAsync() => Task.CompletedTask; } } diff --git a/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs b/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs index f986358..2fefae6 100644 --- a/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs +++ b/src/Serilog.Sinks.Splunk/SplunkLoggingConfigurationExtensions.cs @@ -19,6 +19,7 @@ using Serilog.Core; using Serilog.Events; using Serilog.Formatting; +using Serilog.Sinks.PeriodicBatching; using Serilog.Sinks.Splunk; namespace Serilog @@ -76,14 +77,12 @@ public static LoggerConfiguration EventCollector( sourceType, host, index, - batchIntervalInSeconds, - batchSizeLimit, - queueLimit, formatProvider, renderTemplate, messageHandler); - return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch); + return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch, + batchIntervalInSeconds, batchSizeLimit, queueLimit); } /// @@ -122,13 +121,12 @@ public static LoggerConfiguration EventCollector( splunkHost, eventCollectorToken, uriPath, - batchIntervalInSeconds, - batchSizeLimit, - queueLimit, + jsonFormatter, messageHandler); - return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch); + return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch, + batchIntervalInSeconds, batchSizeLimit, queueLimit); } @@ -183,15 +181,30 @@ public static LoggerConfiguration EventCollector( host, index, fields, - batchIntervalInSeconds, - batchSizeLimit, - queueLimit, formatProvider, renderTemplate, messageHandler ); - return configuration.Sink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch); + return configuration.BuildPeriodicBatchingSink(eventCollectorSink, restrictedToMinimumLevel, levelSwitch, + batchIntervalInSeconds, batchSizeLimit, queueLimit); + } + + private static LoggerConfiguration BuildPeriodicBatchingSink(this LoggerSinkConfiguration configuration, + EventCollectorSink eventCollectorSink, + LogEventLevel restrictedToMinimumLevel, + LoggingLevelSwitch levelSwitch = null, + int batchIntervalInSeconds = 2, + int batchSizeLimit = 100, + int? queueLimit = EventCollectorSink.DefaultQueueLimit) + { + var periodicBatchingOptions = new PeriodicBatchingSinkOptions + { + Period = TimeSpan.FromSeconds(batchIntervalInSeconds), QueueLimit = queueLimit, BatchSizeLimit = batchSizeLimit + }; + var periodicBatchSink = new PeriodicBatchingSink(eventCollectorSink, periodicBatchingOptions); + + return configuration.Sink(periodicBatchSink, restrictedToMinimumLevel, levelSwitch); } } } \ No newline at end of file