Skip to content

Commit

Permalink
Upgrade Serilog to 4.0.x (#212)
Browse files Browse the repository at this point in the history
* Upgrade Serilog to 4.0.x
- use new wrapper sink construction
- remove Serilog.Sinks.PeriodicBatching
- use IBatchedLogEventSink from core serilog package

* Fix Run tests Actions
  • Loading branch information
ArieGato authored Oct 5, 2024
1 parent e0d3aed commit 5a0b443
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
if: ${{ startsWith(matrix.os, 'ubuntu') }}
shell: pwsh
run: |
docker-compose up -d
docker compose up -d
$val = 0
while(1 -eq 1) {
$status_ssl_plain = docker inspect --format "{{json .State.Health.Status }}" serilog.sinks.rabbitmq.ssl-plain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions samples/Net8FromCodeSample/Net8FromCodeSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.AppSettings" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;
using Serilog.Sinks.RabbitMQ;

namespace Serilog;
Expand Down Expand Up @@ -152,7 +151,7 @@ public static LoggerConfiguration RabbitMQ(
MaxChannels = maxChannels,
};

if (sslEnabled)
if (sslEnabled && sslServerName is not null)
{
clientConfiguration.SslOption = new SslOption
{
Expand Down Expand Up @@ -279,7 +278,7 @@ public static LoggerConfiguration RabbitMQ(
MaxChannels = maxChannels,
};

if (sslEnabled)
if (sslEnabled && sslServerName is not null)
{
clientConfiguration.SslOption = new SslOption
{
Expand Down Expand Up @@ -332,11 +331,8 @@ private static LoggerConfiguration RegisterSink(
return loggerSinkConfiguration.Sink(periodicBatchingSink, sinkConfiguration.RestrictedToMinimumLevel);
}

return LoggerSinkConfiguration.Wrap(
loggerSinkConfiguration,
failureSink => GetPeriodicBatchingSink(clientConfiguration, sinkConfiguration, failureSink),
failureSinkConfiguration,
sinkConfiguration.RestrictedToMinimumLevel);
var wrapper = LoggerSinkConfiguration.Wrap(sink => GetPeriodicBatchingSink(clientConfiguration, sinkConfiguration, sink), failureSinkConfiguration);
return loggerSinkConfiguration.Sink(wrapper, sinkConfiguration.RestrictedToMinimumLevel);
}

private static LoggerConfiguration RegisterAuditSink(
Expand All @@ -355,16 +351,16 @@ private static LoggerConfiguration RegisterAuditSink(
.Sink(new RabbitMQSink(clientConfiguration, sinkConfiguration), sinkConfiguration.RestrictedToMinimumLevel);
}

private static PeriodicBatchingSink GetPeriodicBatchingSink(
private static ILogEventSink GetPeriodicBatchingSink(
RabbitMQClientConfiguration clientConfiguration,
RabbitMQSinkConfiguration sinkConfiguration,
ILogEventSink? failureSink = null)
{
var rabbitMQSink = new RabbitMQSink(clientConfiguration, sinkConfiguration, failureSink);
var periodicBatchingSinkOptions = new PeriodicBatchingSinkOptions
var periodicBatchingSinkOptions = new BatchingOptions
{
BatchSizeLimit = sinkConfiguration.BatchPostingLimit,
Period = sinkConfiguration.Period,
BufferingTimeLimit = sinkConfiguration.Period,
EagerlyEmitFirstEvent = true,
};

Expand All @@ -373,7 +369,7 @@ private static PeriodicBatchingSink GetPeriodicBatchingSink(
periodicBatchingSinkOptions.QueueLimit = sinkConfiguration.QueueLimit.Value;
}

return new PeriodicBatchingSink(rabbitMQSink, periodicBatchingSinkOptions);
return LoggerSinkConfiguration.CreateSink(lc => lc.Sink(rabbitMQSink, periodicBatchingSinkOptions));
}

private static void ValidateRabbitMQClientConfiguration(RabbitMQClientConfiguration clientConfiguration)
Expand Down
15 changes: 7 additions & 8 deletions src/Serilog.Sinks.RabbitMQ/Serilog.Sinks.RabbitMQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,27 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" AllowedVersions="[6.8.1,7.0)" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="4.1.0" />
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="All" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
<PackageReference Include="MinVer" Version="6.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.1.32" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.28" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.33" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="7.0.17" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="7.0.20" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.8" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/Serilog.Sinks.RabbitMQ/Sinks/RabbitMQ/RabbitMQSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog.Sinks.RabbitMQ;

Expand Down Expand Up @@ -77,7 +76,7 @@ public void Emit(LogEvent logEvent)
}

/// <inheritdoc cref="IBatchedLogEventSink.EmitBatchAsync" />
public Task EmitBatchAsync(IEnumerable<LogEvent> batch)
public Task EmitBatchAsync(IReadOnlyCollection<LogEvent> batch)
{
// make sure we have an array to avoid multiple enumeration
var logEvents = batch as LogEvent[] ?? batch.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Settings.AppSettings" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -37,8 +37,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="xunit" Version="2.8.0" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Update="xunit" Version="2.9.1" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ namespace Serilog.Sinks.RabbitMQ
NonDurable = 1,
Durable = 2,
}
public sealed class RabbitMQSink : Serilog.Core.ILogEventSink, Serilog.Sinks.PeriodicBatching.IBatchedLogEventSink, System.IDisposable
public sealed class RabbitMQSink : Serilog.Core.IBatchedLogEventSink, Serilog.Core.ILogEventSink, System.IDisposable
{
public void Dispose() { }
public void Emit(Serilog.Events.LogEvent logEvent) { }
public System.Threading.Tasks.Task EmitBatchAsync(System.Collections.Generic.IEnumerable<Serilog.Events.LogEvent> batch) { }
public System.Threading.Tasks.Task EmitBatchAsync(System.Collections.Generic.IReadOnlyCollection<Serilog.Events.LogEvent> batch) { }
public System.Threading.Tasks.Task OnEmptyBatchAsync() { }
}
public class RabbitMQSinkConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Emit_ShouldPublishMessages()
public async Task EmitBatchAsync_ShouldPublishMessages()
{
// Arrange
IEnumerable<LogEvent> logEvents = [
IReadOnlyCollection<LogEvent> logEvents = [
new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate("some-message-1", []), []),
new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate("some-message-2", []), [])];

Expand All @@ -78,7 +78,7 @@ public async Task EmitBatchAsync_ShouldPublishMessages()
public async Task EmitBatchAsync_ShouldDoNothing_WhenNoEventsAreEmitted()
{
// Arrange
IEnumerable<LogEvent> logEvents = [];
IReadOnlyCollection<LogEvent> logEvents = [];

var textFormatter = Substitute.For<ITextFormatter>();
var rabbitMQClient = Substitute.For<IRabbitMQClient>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="xunit" Version="2.8.0" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Update="xunit" Version="2.9.1" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 5a0b443

Please sign in to comment.