Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Start()
private void LogEvent(EventWrittenEventArgs eventData)
{
var level = MapLevel(eventData.Level);
var eventId = new EventId(eventData.EventId, eventData.EventName);
var eventId = new EventId(eventData.EventId);

// Special case the Error event so the Exception Details are written correctly
if (eventData.EventId == 3 &&
Expand Down
33 changes: 31 additions & 2 deletions tests/Aspire.RabbitMQ.Client.Tests/AspireRabbitMQLoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ public void TestInfoAndWarn()
Assert.Equal(warningMessage, logs[1].Message);
}

[Fact]
public void ForwardsEventIdWithoutLevelName()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Services.AddSingleton<RabbitMQEventSourceLogForwarder>();

var logger = new TestLogger();
builder.Services.AddSingleton<ILoggerProvider>(sp => new LoggerProvider(logger));

using var host = builder.Build();
host.Services.GetRequiredService<RabbitMQEventSourceLogForwarder>().Start();

LogInfo("info");
LogWarn("warn");
LogError("error", new InvalidOperationException("test"));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other unit tests in this file don’t wait after Log*. EventSource forwarding is synchronous once the listener is started. Happy to restructure if maintainers prefer a different pattern.

var logs = logger.Logs.ToArray();
Assert.Equal(3, logs.Length);

Assert.Equal(1, logs[0].EventId.Id);
Assert.True(string.IsNullOrEmpty(logs[0].EventId.Name));

Assert.Equal(2, logs[1].EventId.Id);
Assert.True(string.IsNullOrEmpty(logs[1].EventId.Name));

Assert.Equal(3, logs[2].EventId.Id);
Assert.True(string.IsNullOrEmpty(logs[2].EventId.Name));
}

[Fact]
public void TestExceptionWithoutInnerException()
{
Expand Down Expand Up @@ -239,7 +268,7 @@ public void Dispose() { }

private sealed class TestLogger : ILogger
{
public BlockingCollection<(LogLevel Level, string Message, object? State)> Logs { get; } = new();
public BlockingCollection<(LogLevel Level, string Message, object? State, EventId EventId)> Logs { get; } = new();
public Action? LoggedMessage { get; set; }

public IDisposable? BeginScope<TState>(TState state) where TState : notnull =>
Expand All @@ -249,7 +278,7 @@ private sealed class TestLogger : ILogger

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
Logs.Add((logLevel, formatter(state, exception), state));
Logs.Add((logLevel, formatter(state, exception), state, eventId));
LoggedMessage?.Invoke();
}
}
Expand Down
Loading