Skip to content

Commit

Permalink
Merge pull request #128 from nblumhardt/pbs-update
Browse files Browse the repository at this point in the history
Update to Serilog.Sinks.PeriodicBatching 4.0.0
  • Loading branch information
wparad committed Mar 15, 2024
2 parents 9d64f99 + 578040d commit ed21a96
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
env:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
run: |
buildNumber="4.1.${GITHUB_RUN_NUMBER}"
buildNumber="4.2.${GITHUB_RUN_NUMBER}"
sed "s/0.0.1/${buildNumber}/g" src/Serilog.Sinks.AwsCloudWatch/*.csproj -i
dotnet pack -c Release -o artifacts || exit 1
dotnet nuget push artifacts/Serilog.Sinks.AwsCloudWatch.${buildNumber}.nupkg -s "https://api.nuget.org/v3/index.json" -k "$NUGET_KEY"
Expand Down
35 changes: 31 additions & 4 deletions src/Serilog.Sinks.AwsCloudWatch/CloudWatchLogSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Serilog.Core;

namespace Serilog.Sinks.AwsCloudWatch
{
/// <summary>
/// A Serilog log sink that publishes to AWS CloudWatch Logs
/// </summary>
/// <seealso cref="Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink" />
public class CloudWatchLogSink : PeriodicBatchingSink
public class CloudWatchLogSink : ILogEventSink, IBatchedLogEventSink, IDisposable, IAsyncDisposable
{
/// <summary>
/// The maximum log event size = 256 KB - 26 B
Expand Down Expand Up @@ -55,6 +56,7 @@ public class CloudWatchLogSink : PeriodicBatchingSink
private string logStreamName;
private string nextSequenceToken;
private readonly ITextFormatter textFormatter;
private readonly PeriodicBatchingSink batchingSink;

private readonly SemaphoreSlim syncObject = new SemaphoreSlim(1);

Expand All @@ -64,7 +66,7 @@ public class CloudWatchLogSink : PeriodicBatchingSink
/// </summary>
/// <param name="cloudWatchClient">The cloud watch client.</param>
/// <param name="options">The options.</param>
public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, ICloudWatchSinkOptions options) : base(options.BatchSizeLimit, options.Period, options.QueueSizeLimit)
public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, ICloudWatchSinkOptions options)
{
if (string.IsNullOrEmpty(options?.LogGroupName))
{
Expand All @@ -79,10 +81,11 @@ public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, ICloudWatchSink

if (options.TextFormatter == null)
{
throw new System.ArgumentException($"{nameof(options.TextFormatter)} is required");
throw new ArgumentException($"{nameof(options.TextFormatter)} is required");
}

textFormatter = options.TextFormatter;
batchingSink = new(this, new() { BatchSizeLimit = options.BatchSizeLimit, Period = options.Period, QueueLimit = options.QueueSizeLimit });
}
#pragma warning restore CS0618

Expand Down Expand Up @@ -340,7 +343,7 @@ private async Task PublishBatchAsync(List<InputLogEvent> batch)
/// Emit a batch of log events, running asynchronously.
/// </summary>
/// <param name="events">The events to emit.</param>
protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
async Task IBatchedLogEventSink.EmitBatchAsync(IEnumerable<LogEvent> events)
{
try
{
Expand Down Expand Up @@ -415,5 +418,29 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
syncObject.Release();
}
}

/// <inheritdoc/>
Task IBatchedLogEventSink.OnEmptyBatchAsync()
{
return Task.CompletedTask;
}

/// <inheritdoc/>
public void Emit(LogEvent logEvent)
{
batchingSink.Emit(logEvent);
}

/// <inheritdoc/>
public void Dispose()
{
batchingSink.Dispose();
}

/// <inheritdoc/>
public ValueTask DisposeAsync()
{
return batchingSink.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="[3.5.0.9,)" />
<PackageReference Include="Serilog" Version="[2.5.0,)" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="[2.1.1,)" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="[3.1.0,)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog.Sinks.AwsCloudWatch.Tests
{
Expand All @@ -17,8 +15,7 @@ public static class CloudWatchLogsSinkExtensions
/// <returns>The task to wait on.</returns>
public static Task EmitBatchAsync(this CloudWatchLogSink sink, IEnumerable<LogEvent> events)
{
var emitMethod = sink.GetType().GetMethod("EmitBatchAsync", BindingFlags.NonPublic | BindingFlags.Instance);
return emitMethod.Invoke(sink, new object[] { events }) as Task;
return ((IBatchedLogEventSink)sink).EmitBatchAsync(events);
}
}
}

0 comments on commit ed21a96

Please sign in to comment.