Skip to content

Commit

Permalink
Logging fixes (#413)
Browse files Browse the repository at this point in the history
When using `EventId` as a replaceable parameter in logging templates, it gets replaced by a value of `Microsoft.Extensions.Logging.EventId` which isn't the desired behavior. This PR fixes that by renaming the parameter to `EventBusId` or `EventBusIds`.
  • Loading branch information
mburumaxwell authored Apr 25, 2022
1 parent afb96d6 commit 422f664
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
};

// send the event
Logger.SendingToStream(eventId: @event.Id, streamName: streamName, scheduled: scheduled);
Logger.SendingToStream(eventBusId: @event.Id, streamName: streamName, scheduled: scheduled);
var response = await kinesisClient.PutRecordAsync(request, cancellationToken);
response.EnsureSuccess();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ namespace Microsoft.Extensions.Logging;
/// </summary>
internal static partial class ILoggerExtensions
{
[LoggerMessage(100, LogLevel.Information, "Sending {EventId} to '{StreamName}'. Scheduled: {Scheduled}.")]
public static partial void SendingToStream(this ILogger logger, string? eventId, string streamName, DateTimeOffset? scheduled);
[LoggerMessage(100, LogLevel.Information, "Sending {EventBusId} to '{StreamName}'. Scheduled: {Scheduled}.")]
public static partial void SendingToStream(this ILogger logger, string? eventBusId, string streamName, DateTimeOffset? scheduled);

[LoggerMessage(101, LogLevel.Information, "Sending {EventsCount} messages to '{StreamName}'. Scheduled: {Scheduled}. Events:\r\n- {EventIds}")]
private static partial void SendingEventsToStream(this ILogger logger, int eventsCount, string streamName, DateTimeOffset? scheduled, string eventIds);
[LoggerMessage(101, LogLevel.Information, "Sending {EventsCount} messages to '{StreamName}'. Scheduled: {Scheduled}. Events:\r\n- {EventBusIds}")]
private static partial void SendingEventsToStream(this ILogger logger, int eventsCount, string streamName, DateTimeOffset? scheduled, string eventBusIds);

public static void SendingEventsToStream(this ILogger logger, IList<string?> eventIds, string streamName, DateTimeOffset? scheduled)
public static void SendingEventsToStream(this ILogger logger, IList<string?> eventBusIds, string streamName, DateTimeOffset? scheduled)
{
if (!logger.IsEnabled(LogLevel.Information)) return;
logger.SendingEventsToStream(eventsCount: eventIds.Count,
logger.SendingEventsToStream(eventsCount: eventBusIds.Count,
streamName: streamName,
scheduled: scheduled,
eventIds: string.Join("\r\n- ", eventIds));
eventBusIds: string.Join("\r\n- ", eventBusIds));
}

public static void SendingEventsToStream<T>(this ILogger logger, IList<EventContext<T>> events, string entityPath, DateTimeOffset? scheduled = null)
Expand Down
12 changes: 6 additions & 6 deletions src/Tingle.EventBus.Transports.Amazon.Sqs/AmazonSqsTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
// get the topic arn and send the message
var topicArn = await GetTopicArnAsync(registration, cancellationToken);
var request = new PublishRequest(topicArn: topicArn, message: body.ToString()).SetAttributes(@event);
Logger.SendingToTopic(eventId: @event.Id, topicArn: topicArn, scheduled: scheduled);
Logger.SendingToTopic(eventBusId: @event.Id, topicArn: topicArn, scheduled: scheduled);
var response = await snsClient.PublishAsync(request: request, cancellationToken: cancellationToken);
response.EnsureSuccess();
sequenceNumber = response.SequenceNumber;
Expand All @@ -137,7 +137,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
// cap the delay to 900 seconds (15min) which is the max supported by SQS
if (delay > 900)
{
Logger.DelayCapped(eventId: @event.Id, scheduled: scheduled);
Logger.DelayCapped(eventBusId: @event.Id, scheduled: scheduled);
delay = 900;
}

Expand All @@ -148,7 +148,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
}

// send the message
Logger.SendingToQueue(eventId: @event.Id, queueUrl: queueUrl, scheduled: scheduled);
Logger.SendingToQueue(eventBusId: @event.Id, queueUrl: queueUrl, scheduled: scheduled);
var response = await sqsClient.SendMessageAsync(request: request, cancellationToken: cancellationToken);
response.EnsureSuccess();
sequenceNumber = response.SequenceNumber;
Expand Down Expand Up @@ -190,7 +190,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
// get the topic arn and send the message
var topicArn = await GetTopicArnAsync(registration, cancellationToken);
var request = new PublishRequest(topicArn: topicArn, message: body.ToString()).SetAttributes(@event);
Logger.SendingToTopic(eventId: @event.Id, topicArn: topicArn, scheduled: scheduled);
Logger.SendingToTopic(eventBusId: @event.Id, topicArn: topicArn, scheduled: scheduled);
var response = await snsClient.PublishAsync(request: request, cancellationToken: cancellationToken);
response.EnsureSuccess();

Expand Down Expand Up @@ -219,7 +219,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
// cap the delay to 900 seconds (15min) which is the max supported by SQS
if (delay > 900)
{
Logger.DelayCapped(eventId: @event.Id, scheduled: scheduled);
Logger.DelayCapped(eventBusId: @event.Id, scheduled: scheduled);
delay = 900;
}

Expand Down Expand Up @@ -473,7 +473,7 @@ private async Task OnMessageReceivedAsync<TEvent, TConsumer>(EventRegistration r
raw: message,
cancellationToken: cancellationToken);

Logger.ReceivedMessage(messageId: messageId, eventId: context.Id, queueUrl: queueUrl);
Logger.ReceivedMessage(messageId: messageId, eventBusId: context.Id, queueUrl: queueUrl);

var (successful, _) = await ConsumeAsync<TEvent, TConsumer>(ecr: ecr,
@event: context,
Expand Down
16 changes: 8 additions & 8 deletions src/Tingle.EventBus.Transports.Amazon.Sqs/ILoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ internal static partial class ILoggerExtensions
public static partial void BatchingNotSupported(this ILogger logger);


[LoggerMessage(200, LogLevel.Information, "Sending {EventId} to '{TopicArn}'. Scheduled: {Scheduled}.")]
public static partial void SendingToTopic(this ILogger logger, string? eventId, string topicArn, DateTimeOffset? scheduled);
[LoggerMessage(200, LogLevel.Information, "Sending {EventBusId} to '{TopicArn}'. Scheduled: {Scheduled}.")]
public static partial void SendingToTopic(this ILogger logger, string? eventBusId, string topicArn, DateTimeOffset? scheduled);

[LoggerMessage(201, LogLevel.Information, "Sending {EventId} to '{QueueUrl}'. Scheduled: {Scheduled}.")]
public static partial void SendingToQueue(this ILogger logger, string? eventId, string queueUrl, DateTimeOffset? scheduled);
[LoggerMessage(201, LogLevel.Information, "Sending {EventBusId} to '{QueueUrl}'. Scheduled: {Scheduled}.")]
public static partial void SendingToQueue(this ILogger logger, string? eventBusId, string queueUrl, DateTimeOffset? scheduled);

[LoggerMessage(202, LogLevel.Warning, "Delay for {EventId} capped at 15min. Scheduled: {Scheduled}.")]
public static partial void DelayCapped(this ILogger logger, string? eventId, DateTimeOffset? scheduled);
[LoggerMessage(202, LogLevel.Warning, "Delay for {EventBusId} capped at 15min. Scheduled: {Scheduled}.")]
public static partial void DelayCapped(this ILogger logger, string? eventBusId, DateTimeOffset? scheduled);


[LoggerMessage(300, LogLevel.Trace, "No messages on '{QueueUrl}', delaying check for {Delay}.")]
Expand All @@ -28,8 +28,8 @@ internal static partial class ILoggerExtensions
[LoggerMessage(301, LogLevel.Debug, "Received {MessagesCount} messages on '{QueueUrl}'")]
public static partial void ReceivedMessages(this ILogger logger, int messagesCount, string queueUrl);

[LoggerMessage(302, LogLevel.Information, "Received message: '{MessageId}' containing Event '{EventId}' from '{QueueUrl}'")]
public static partial void ReceivedMessage(this ILogger logger, string messageId, string? eventId, string queueUrl);
[LoggerMessage(302, LogLevel.Information, "Received message: '{MessageId}' containing Event '{EventBusId}' from '{QueueUrl}'")]
public static partial void ReceivedMessage(this ILogger logger, string messageId, string? eventBusId, string queueUrl);

[LoggerMessage(303, LogLevel.Debug, "Processing '{MessageId}' from '{QueueUrl}'")]
public static partial void ProcessingMessage(this ILogger logger, string messageId, string queueUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)

// get the producer and send the event accordingly
var producer = await GetProducerAsync(reg: registration, deadletter: false, cancellationToken: cancellationToken);
Logger.SendingEvent(eventId: @event.Id, eventHubName: producer.EventHubName, scheduled: scheduled);
Logger.SendingEvent(eventBusId: @event.Id, eventHubName: producer.EventHubName, scheduled: scheduled);
await producer.SendAsync(new[] { data }, cancellationToken);

// return the sequence number
Expand Down Expand Up @@ -406,7 +406,7 @@ private async Task OnEventReceivedAsync<TEvent, TConsumer>(EventRegistration reg
identifier: data.SequenceNumber.ToString(),
raw: data,
cancellationToken: cancellationToken);
Logger.ReceivedEvent(eventId: context.Id,
Logger.ReceivedEvent(eventBusId: context.Id,
eventHubName: processor.EventHubName,
consumerGroup: processor.ConsumerGroup,
partitionKey: data.PartitionKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ internal static partial class ILoggerExtensions
[LoggerMessage(201, LogLevel.Warning, "Azure EventHubs does not support expiring events.")]
public static partial void ExpiryNotSupported(this ILogger logger);

[LoggerMessage(202, LogLevel.Information, "Sending {EventId} to '{EventHubName}'. Scheduled: {Scheduled}")]
public static partial void SendingEvent(this ILogger logger, string? eventId, string eventHubName, DateTimeOffset? scheduled);
[LoggerMessage(202, LogLevel.Information, "Sending {EventBusId} to '{EventHubName}'. Scheduled: {Scheduled}")]
public static partial void SendingEvent(this ILogger logger, string? eventBusId, string eventHubName, DateTimeOffset? scheduled);

[LoggerMessage(203, LogLevel.Information, "Sending {EventsCount} events to '{EventHubName}'. Scheduled: {Scheduled}. Events:\r\n- {EventIds}")]
private static partial void SendingEvents(this ILogger logger, int eventsCount, string eventHubName, DateTimeOffset? scheduled, string eventIds);
[LoggerMessage(203, LogLevel.Information, "Sending {EventsCount} events to '{EventHubName}'. Scheduled: {Scheduled}. Events:\r\n- {EventBusIds}")]
private static partial void SendingEvents(this ILogger logger, int eventsCount, string eventHubName, DateTimeOffset? scheduled, string eventBusIds);

public static void SendingEvents(this ILogger logger, IList<string?> eventIds, string eventHubName, DateTimeOffset? scheduled)
public static void SendingEvents(this ILogger logger, IList<string?> eventBusIds, string eventHubName, DateTimeOffset? scheduled)
{
if (!logger.IsEnabled(LogLevel.Information)) return;
logger.SendingEvents(eventsCount: eventIds.Count,
logger.SendingEvents(eventsCount: eventBusIds.Count,
eventHubName: eventHubName,
scheduled: scheduled,
eventIds: string.Join("\r\n- ", eventIds));
eventBusIds: string.Join("\r\n- ", eventBusIds));
}

public static void SendingEvents<T>(this ILogger logger, IList<EventContext<T>> events, string eventHubName, DateTimeOffset? scheduled = null)
Expand All @@ -66,6 +66,6 @@ public static void SendingEvents<T>(this ILogger logger, IList<EventContext<T>>
[LoggerMessage(301, LogLevel.Debug, "Processing '{MessageId}' from '{EventHubName}/{ConsumerGroup}'.\r\nPartitionKey: {PartitionKey}\r\nSequenceNumber: {SequenceNumber}'")]
public static partial void ProcessingEvent(this ILogger logger, string messageId, string eventHubName, string consumerGroup, string partitionKey, long sequenceNumber);

[LoggerMessage(302, LogLevel.Information, "Received event: '{EventId}' from '{EventHubName}/{ConsumerGroup}'.\r\nPartitionKey: {PartitionKey}\r\nSequenceNumber: {SequenceNumber}'")]
public static partial void ReceivedEvent(this ILogger logger, string? eventId, string eventHubName, string consumerGroup, string partitionKey, long sequenceNumber);
[LoggerMessage(302, LogLevel.Information, "Received event: '{EventBusId}' from '{EventHubName}/{ConsumerGroup}'.\r\nPartitionKey: {PartitionKey}\r\nSequenceNumber: {SequenceNumber}'")]
public static partial void ReceivedEvent(this ILogger logger, string? eventBusId, string eventHubName, string consumerGroup, string partitionKey, long sequenceNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)

// get the queue client and send the message
var queueClient = await GetQueueClientAsync(reg: registration, deadletter: false, cancellationToken: cancellationToken);
Logger.SendingMessage(eventId: @event.Id, queueName: queueClient.Name, scheduled: scheduled);
Logger.SendingMessage(eventBusId: @event.Id, queueName: queueClient.Name, scheduled: scheduled);
var response = await queueClient.SendMessageAsync(messageText: body.ToString(),
visibilityTimeout: visibilityTimeout,
timeToLive: ttl,
Expand Down Expand Up @@ -138,7 +138,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
var ttl = @event.Expires - DateTimeOffset.UtcNow;

// send the message
Logger.SendingMessage(eventId: @event.Id, queueName: queueClient.Name, scheduled: scheduled);
Logger.SendingMessage(eventBusId: @event.Id, queueName: queueClient.Name, scheduled: scheduled);
var response = await queueClient.SendMessageAsync(messageText: body.ToString(),
visibilityTimeout: visibilityTimeout,
timeToLive: ttl,
Expand Down Expand Up @@ -336,7 +336,7 @@ private async Task OnMessageReceivedAsync<TEvent, TConsumer>(EventRegistration r
raw: message,
cancellationToken: cancellationToken);

Logger.ReceivedMessage(messageId: messageId, eventId: context.Id, queueName: queueClient.Name);
Logger.ReceivedMessage(messageId: messageId, eventBusId: context.Id, queueName: queueClient.Name);

// if the event contains the parent activity id, set it
if (context.Headers.TryGetValue(HeaderNames.ActivityId, out var parentActivityId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal static partial class ILoggerExtensions
[LoggerMessage(200, LogLevel.Warning, "Azure Queue Storage does not support batching. The events will be looped through one by one.")]
public static partial void BatchingNotSupported(this ILogger logger);

[LoggerMessage(201, LogLevel.Information, "Sending {EventId} to '{QueueName}'. Scheduled: {Scheduled}.")]
public static partial void SendingMessage(this ILogger logger, string? eventId, string queueName, DateTimeOffset? scheduled);
[LoggerMessage(201, LogLevel.Information, "Sending {EventBusId} to '{QueueName}'. Scheduled: {Scheduled}.")]
public static partial void SendingMessage(this ILogger logger, string? eventBusId, string queueName, DateTimeOffset? scheduled);

[LoggerMessage(202, LogLevel.Information, "Cancelling '{MessageId}|{PopReceipt}' on '{QueueName}'.")]
public static partial void CancelingMessage(this ILogger logger, string messageId, string popReceipt, string queueName);
Expand All @@ -31,8 +31,8 @@ internal static partial class ILoggerExtensions
[LoggerMessage(302, LogLevel.Debug, "Processing '{MessageId}' from '{QueueName}'")]
public static partial void ProcessingMessage(this ILogger logger, string messageId, string queueName);

[LoggerMessage(303, LogLevel.Information, "Received message: '{MessageId}' containing Event '{EventId}' from '{QueueName}'")]
public static partial void ReceivedMessage(this ILogger logger, string messageId, string? eventId, string queueName);
[LoggerMessage(303, LogLevel.Information, "Received message: '{MessageId}' containing Event '{EventBusId}' from '{QueueName}'")]
public static partial void ReceivedMessage(this ILogger logger, string messageId, string? eventBusId, string queueName);

[LoggerMessage(304, LogLevel.Trace, "Deleting '{MessageId}' on '{QueueName}'")]
public static partial void DeletingMessage(this ILogger logger, string messageId, string queueName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)

// Get the sender and send the message accordingly
var sender = await GetSenderAsync(registration, cancellationToken);
Logger.SendingMessage(eventId: @event.Id, entityPath: sender.EntityPath, scheduled: scheduled);
Logger.SendingMessage(eventBusId: @event.Id, entityPath: sender.EntityPath, scheduled: scheduled);
if (scheduled != null)
{
var seqNum = await sender.ScheduleMessageAsync(message: message,
Expand Down Expand Up @@ -538,7 +538,7 @@ private async Task OnMessageReceivedAsync<TEvent, TConsumer>(EventRegistration r
raw: message,
cancellationToken: cancellationToken);

Logger.ReceivedMessage(sequenceNumber: message.SequenceNumber, eventId: context.Id, entityPath: entityPath);
Logger.ReceivedMessage(sequenceNumber: message.SequenceNumber, eventBusId: context.Id, entityPath: entityPath);

// set the extras
context.SetServiceBusReceivedMessage(message);
Expand All @@ -550,7 +550,7 @@ private async Task OnMessageReceivedAsync<TEvent, TConsumer>(EventRegistration r

// Decide the action to execute then execute
var action = DecideAction(successful, ecr.UnhandledErrorBehaviour, processor.AutoCompleteMessages);
Logger.PostConsumeAction(action: action, messageId: messageId, entityPath: entityPath, eventId: context.Id);
Logger.PostConsumeAction(action: action, messageId: messageId, entityPath: entityPath, eventBusId: context.Id);

if (action == PostConsumeAction.Complete)
{
Expand Down
Loading

0 comments on commit 422f664

Please sign in to comment.