Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/SquidStd.Services.Core/Services/EventBusService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using Serilog;
using Serilog.Events;
using SquidStd.Core.Data.Events;
using SquidStd.Core.Interfaces.Events;
using SquidStd.Services.Core.Services.Internal;
Expand Down Expand Up @@ -48,6 +49,11 @@ public async Task PublishAsync<TEvent>(TEvent eventData, CancellationToken cance

var total = (typed?.Length ?? 0) + (global?.Length ?? 0);

if (_logger.IsEnabled(LogEventLevel.Verbose))
{
_logger.Verbose("Publishing {EventType} to {ListenerCount} listener(s)", typeof(TEvent).Name, total);
}

if (total == 0)
{
return;
Expand Down
42 changes: 42 additions & 0 deletions tests/SquidStd.Tests/Services/Core/EventBusServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,48 @@ public async Task HandleAsync(TestEvent eventData, CancellationToken cancellatio
[Collection(SerilogEventSinkCollection.Name)]
public class Telemetry
{
[Fact]
public async Task PublishAsync_AtVerbose_TracesEventFlow()
{
var sink = new CapturingLogSink();
var original = Log.Logger;
Log.Logger = new LoggerConfiguration()
.MinimumLevel
.Verbose()
.WriteTo
.Sink(sink)
.CreateLogger();

try
{
using var eventBus = new EventBusService(new());
IEventBus bus = eventBus;
bus.Subscribe<TestEvent>((_, _) => Task.CompletedTask);

await bus.PublishAsync(new TestEvent("payload"), CancellationToken.None);
await bus.PublishAsync(new OtherEvent(), CancellationToken.None); // zero listeners

Assert.Contains(
sink.Events,
e => e.Level == LogEventLevel.Verbose
&& e.RenderMessage().Contains("TestEvent", StringComparison.Ordinal)
&& e.RenderMessage().Contains("1 listener", StringComparison.Ordinal)
);
Assert.Contains(
sink.Events,
e => e.Level == LogEventLevel.Verbose
&& e.RenderMessage().Contains("OtherEvent", StringComparison.Ordinal)
&& e.RenderMessage().Contains("0 listener", StringComparison.Ordinal)
);
}
finally
{
Log.Logger = original;
}
}

private sealed record OtherEvent : IEvent;

[Fact]
public async Task PublishAsync_SlowListener_LogsWarning()
{
Expand Down
Loading