Skip to content

Commit 04e594f

Browse files
authored
Merge pull request dotnet-architecture#955 from dotnet-architecture/enhancement/improve-event-bus-logging
Add logging of subscription events
2 parents f9388c5 + 76e1072 commit 04e594f

16 files changed

Lines changed: 53 additions & 22 deletions

src/Services/Ordering/Ordering.API/Application/Behaviors/BehaviorsHelperExtensions.cs renamed to src/BuildingBlocks/EventBus/EventBus/Extensions/GenericTypeExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55

6-
namespace Ordering.API.Application.Behaviors
6+
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions
77
{
8-
internal static class BehaviorsHelperExtensions
8+
public static class GenericTypeExtensions
99
{
10-
internal static string GetGenericTypeName(this object @object)
10+
public static string GetGenericTypeName(this Type type)
1111
{
1212
var typeName = string.Empty;
13-
var type = @object.GetType();
1413

1514
if (type.IsGenericType)
1615
{
@@ -25,5 +24,9 @@ internal static string GetGenericTypeName(this object @object)
2524
return typeName;
2625
}
2726

27+
public static string GetGenericTypeName(this object @object)
28+
{
29+
return @object.GetType().GetGenericTypeName();
30+
}
2831
}
2932
}

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
33
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
44
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
5+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
56
using Microsoft.Extensions.Logging;
67
using Newtonsoft.Json;
78
using Newtonsoft.Json.Linq;
@@ -107,6 +108,8 @@ public void Publish(IntegrationEvent @event)
107108
public void SubscribeDynamic<TH>(string eventName)
108109
where TH : IDynamicIntegrationEventHandler
109110
{
111+
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
112+
110113
DoInternalSubscription(eventName);
111114
_subsManager.AddDynamicSubscription<TH>(eventName);
112115
}
@@ -117,6 +120,9 @@ public void Subscribe<T, TH>()
117120
{
118121
var eventName = _subsManager.GetEventKey<T>();
119122
DoInternalSubscription(eventName);
123+
124+
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
125+
120126
_subsManager.AddSubscription<T, TH>();
121127
}
122128

@@ -140,9 +146,13 @@ private void DoInternalSubscription(string eventName)
140146
}
141147

142148
public void Unsubscribe<T, TH>()
143-
where TH : IIntegrationEventHandler<T>
144149
where T : IntegrationEvent
150+
where TH : IIntegrationEventHandler<T>
145151
{
152+
var eventName = _subsManager.GetEventKey<T>();
153+
154+
_logger.LogInformation("Unsubscribing from event {EventName}", eventName);
155+
146156
_subsManager.RemoveSubscription<T, TH>();
147157
}
148158

@@ -215,7 +225,7 @@ private async Task ProcessEvent(string eventName, string message)
215225
foreach (var subscription in subscriptions)
216226
{
217227
if (subscription.IsDynamic)
218-
{
228+
{
219229
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
220230
if (handler == null) continue;
221231
dynamic eventData = JObject.Parse(message);

src/BuildingBlocks/EventBus/EventBusServiceBus/EventBusServiceBus.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public EventBusServiceBus(IServiceBusPersisterConnection serviceBusPersisterConn
2727
ILifetimeScope autofac)
2828
{
2929
_serviceBusPersisterConnection = serviceBusPersisterConnection;
30-
_logger = logger;
30+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3131
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
3232

3333
_subscriptionClient = new SubscriptionClient(serviceBusPersisterConnection.ServiceBusConnectionStringBuilder,
@@ -61,6 +61,8 @@ public void Publish(IntegrationEvent @event)
6161
public void SubscribeDynamic<TH>(string eventName)
6262
where TH : IDynamicIntegrationEventHandler
6363
{
64+
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, nameof(TH));
65+
6466
_subsManager.AddDynamicSubscription<TH>(eventName);
6567
}
6668

@@ -87,6 +89,8 @@ public void Subscribe<T, TH>()
8789
}
8890
}
8991

92+
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, nameof(TH));
93+
9094
_subsManager.AddSubscription<T, TH>();
9195
}
9296

@@ -108,12 +112,16 @@ public void Unsubscribe<T, TH>()
108112
_logger.LogWarning("The messaging entity {eventName} Could not be found.", eventName);
109113
}
110114

115+
_logger.LogInformation("Unsubscribing from event {EventName}", eventName);
116+
111117
_subsManager.RemoveSubscription<T, TH>();
112118
}
113119

114120
public void UnsubscribeDynamic<TH>(string eventName)
115121
where TH : IDynamicIntegrationEventHandler
116122
{
123+
_logger.LogInformation("Unsubscribing from dynamic event {EventName}", eventName);
124+
117125
_subsManager.RemoveDynamicSubscription<TH>(eventName);
118126
}
119127

@@ -136,17 +144,16 @@ private void RegisterSubscriptionClientMessageHandler()
136144
await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
137145
}
138146
},
139-
new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 10, AutoComplete = false });
147+
new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 10, AutoComplete = false });
140148
}
141149

142150
private Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
143151
{
144-
Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
152+
var ex = exceptionReceivedEventArgs.Exception;
145153
var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
146-
Console.WriteLine("Exception context for troubleshooting:");
147-
Console.WriteLine($"- Endpoint: {context.Endpoint}");
148-
Console.WriteLine($"- Entity Path: {context.EntityPath}");
149-
Console.WriteLine($"- Executing Action: {context.Action}");
154+
155+
_logger.LogError(ex, "ERROR handling message: {ExceptionMessage} - Context: {@ExceptionContext}", ex.Message, context);
156+
150157
return Task.CompletedTask;
151158
}
152159

@@ -172,7 +179,7 @@ private async Task<bool> ProcessEvent(string eventName, string message)
172179
var handler = scope.ResolveOptional(subscription.HandlerType);
173180
if (handler == null) continue;
174181
var eventType = _subsManager.GetEventTypeByName(eventName);
175-
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
182+
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
176183
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
177184
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
178185
}

src/Services/Basket/Basket.FunctionalTests/BasketScenarios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Net.Http;
66
using System.Text;
77
using System.Threading.Tasks;
8-
using WebMVC.ServicesModelDTOs;
8+
using WebMVC.Services.ModelDTOs;
99
using Xunit;
1010

1111
namespace Basket.FunctionalTests

src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
67

78
namespace Ordering.API.Application.Behaviors
89
{

src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
22
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
34
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
45
using Microsoft.Extensions.Logging;
56
using Ordering.API.Application.IntegrationEvents;

src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
89

910
namespace Ordering.API.Application.Behaviors
1011
{

src/Services/Ordering/Ordering.API/Application/Commands/IdentifiedCommandHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MediatR;
2+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
23
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
34
using Microsoft.Extensions.Logging;
45
using Ordering.API.Application.Behaviors;

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MediatR;
22
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
3+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
34
using Microsoft.eShopOnContainers.Services.Ordering.API;
45
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
56
using Microsoft.Extensions.Logging;

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
{
33
using MediatR;
44
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
5+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
56
using Microsoft.eShopOnContainers.Services.Ordering.API;
67
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
78
using Microsoft.Extensions.Logging;
89
using Ordering.API.Application.Behaviors;
910
using Ordering.API.Application.Commands;
1011
using Ordering.API.Application.IntegrationEvents.Events;
1112
using Serilog.Context;
12-
using System;
1313
using System.Threading.Tasks;
14+
using System;
1415

1516
public class OrderPaymentFailedIntegrationEventHandler :
1617
IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>

0 commit comments

Comments
 (0)