Skip to content

Commit

Permalink
Merge pull request #219 from clickviewapp/QueueHostedService-logscope
Browse files Browse the repository at this point in the history
Add LogScope to QueueHostedService to add the message id
  • Loading branch information
Prads- authored Nov 28, 2024
2 parents 49f9274 + 6726f7b commit b366da2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Hosting/Queue/src/MessageContextScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ClickView.GoodStuff.Hosting.Queue;

using System.Collections;
using Queues.RabbitMq;

internal class MessageContextScope<T>(MessageContext<T> context) : IReadOnlyList<KeyValuePair<string, object>>
{
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (var i = 0; i < Count; ++i)
{
yield return this[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public int Count => 1;

public KeyValuePair<string, object> this[int index] => index switch
{
0 => new KeyValuePair<string, object>("MessageId", context.Id),
_ => throw new IndexOutOfRangeException(nameof(index)),
};
}
2 changes: 2 additions & 0 deletions src/Hosting/Queue/src/QueueHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ protected override Task<SubscriptionContext> SubscribeAsync(IQueueClient queueCl
/// <param name="cancellationToken"></param>
private async Task OnMessageAsync(MessageContext<TMessage> messageContext, CancellationToken cancellationToken)
{
using var logScope = Logger.BeginScope(new MessageContextScope<TMessage>(messageContext));

Logger.QueueMessageReceived(messageContext.Id, messageContext.Timestamp);

try
Expand Down

0 comments on commit b366da2

Please sign in to comment.