From e8262539decec731082ee846266efec3b343dab7 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 14 Mar 2019 15:10:08 +0800 Subject: [PATCH] Fix non-structured traces --- .../Basket/Basket.API/Basket.API.csproj | 6 ------ .../Controllers/BasketController.cs | 20 ++++--------------- .../OrderStartedIntegrationEventHandler.cs | 7 +++---- ...ductPriceChangedIntegrationEventHandler.cs | 9 ++++----- .../Events/OrderStartedIntegrationEvent.cs | 6 ++---- .../ProductPriceChangedIntegrationEvent.cs | 6 ++---- .../UserCheckoutAcceptedIntegrationEvent.cs | 5 ++--- .../Application/BasketWebApiTest.cs | 14 ++++++------- .../AutofacModules/ApplicationModule.cs | 11 ++-------- ...angedToCancelledIntegrationEventHandler.cs | 4 ++-- ...tusChangedToPaidIntegrationEventHandler.cs | 4 ++-- ...ChangedToShippedIntegrationEventHandler.cs | 4 ++-- 12 files changed, 31 insertions(+), 65 deletions(-) diff --git a/src/Services/Basket/Basket.API/Basket.API.csproj b/src/Services/Basket/Basket.API/Basket.API.csproj index 262e0afa7..d2af28023 100644 --- a/src/Services/Basket/Basket.API/Basket.API.csproj +++ b/src/Services/Basket/Basket.API/Basket.API.csproj @@ -41,10 +41,4 @@ - - - - - - diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 7bab4e969..8c2d21e50 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -2,14 +2,13 @@ using Basket.API.Model; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.eShopOnContainers.Services.Basket.API.Services; using Microsoft.Extensions.Logging; -using Serilog.Context; using System; using System.Net; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers { @@ -20,14 +19,14 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers { private readonly IBasketRepository _repository; private readonly IIdentityService _identityService; - private readonly IEventBus _eventBus; + private readonly ICapPublisher _eventBus; private readonly ILogger _logger; public BasketController( ILogger logger, IBasketRepository repository, IIdentityService identityService, - IEventBus eventBus) + ICapPublisher eventBus) { _logger = logger; _repository = repository; @@ -78,18 +77,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers // Once basket is checkout, sends an integration event to // ordering.api to convert basket to order and proceeds with // order creation process - try - { - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppName, eventMessage); - - _eventBus.Publish(eventMessage); - } - catch (Exception ex) - { - _logger.LogError(ex, "ERROR Publishing integration event: {IntegrationEventId} from {AppName}", eventMessage.Id, Program.AppName); - - throw; - } + _eventBus.Publish(nameof(UserCheckoutAcceptedIntegrationEvent), eventMessage); return Accepted(); } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs index 13f79740c..2dc77068f 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs @@ -1,5 +1,4 @@ using Basket.API.IntegrationEvents.Events; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Basket.API; using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.Extensions.Logging; @@ -10,7 +9,7 @@ using DotNetCore.CAP; namespace Basket.API.IntegrationEvents.EventHandling { - public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler, ICapSubscribe + public class OrderStartedIntegrationEventHandler : ICapSubscribe { private readonly IBasketRepository _repository; private readonly ILogger _logger; @@ -26,9 +25,9 @@ namespace Basket.API.IntegrationEvents.EventHandling //TODO: [CapSubscribe(nameof(OrderStartedIntegrationEvent))] public async Task Handle(OrderStartedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", Program.AppName, @event); await _repository.DeleteBasketAsync(@event.UserId.ToString()); } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs index 272d84ced..2a5a99620 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs @@ -1,5 +1,4 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events; +using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events; using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.Extensions.Logging; using Serilog.Context; @@ -10,7 +9,7 @@ using DotNetCore.CAP; namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling { - public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler, ICapSubscribe + public class ProductPriceChangedIntegrationEventHandler : ICapSubscribe { private readonly ILogger _logger; private readonly IBasketRepository _repository; @@ -26,9 +25,9 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even //TODO: [CapSubscribe(nameof(ProductPriceChangedIntegrationEvent))] public async Task Handle(ProductPriceChangedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var userIds = _repository.GetUsers(); diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs index b78d10c05..185d0490c 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs @@ -1,11 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Basket.API.IntegrationEvents.Events +namespace Basket.API.IntegrationEvents.Events { // Integration Events notes: // An Event is “something that has happened in the past”, therefore its name has to be // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems. - public class OrderStartedIntegrationEvent : IntegrationEvent + public class OrderStartedIntegrationEvent { public string UserId { get; set; } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs index 6f51010be..daf44ed7b 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs @@ -1,11 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events +namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events { // Integration Events notes: // An Event is “something that has happened in the past”, therefore its name has to be // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems. - public class ProductPriceChangedIntegrationEvent : IntegrationEvent + public class ProductPriceChangedIntegrationEvent { public int ProductId { get; private set; } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs index 18d522cf6..fa5a7730f 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs @@ -1,10 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using Microsoft.eShopOnContainers.Services.Basket.API.Model; +using Microsoft.eShopOnContainers.Services.Basket.API.Model; using System; namespace Basket.API.IntegrationEvents.Events { - public class UserCheckoutAcceptedIntegrationEvent : IntegrationEvent + public class UserCheckoutAcceptedIntegrationEvent { public string UserId { get; } diff --git a/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs b/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs index 0045ce4aa..651d99e9f 100644 --- a/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs +++ b/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs @@ -2,7 +2,6 @@ using Basket.API.Model; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Basket.API.Controllers; using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.Extensions.Logging; @@ -11,6 +10,7 @@ using System; using System.Collections.Generic; using System.Security.Claims; using System.Threading.Tasks; +using DotNetCore.CAP; using Xunit; using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.Services.IIdentityService; @@ -20,14 +20,14 @@ namespace UnitTest.Basket.Application { private readonly Mock _basketRepositoryMock; private readonly Mock _identityServiceMock; - private readonly Mock _serviceBusMock; + private readonly Mock _serviceBusMock; private readonly Mock> _loggerMock; public BasketWebApiTest() { _basketRepositoryMock = new Mock(); _identityServiceMock = new Mock(); - _serviceBusMock = new Mock(); + _serviceBusMock = new Mock(); _loggerMock = new Mock>(); } @@ -42,7 +42,7 @@ namespace UnitTest.Basket.Application .Returns(Task.FromResult(fakeCustomerBasket)); _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); - _serviceBusMock.Setup(x => x.Publish(It.IsAny())); + _serviceBusMock.Setup(x => x.Publish(nameof(UserCheckoutAcceptedIntegrationEvent), It.IsAny(), null)); //Act var basketController = new BasketController( @@ -54,7 +54,6 @@ namespace UnitTest.Basket.Application var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId); //Assert - Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId); } @@ -68,7 +67,7 @@ namespace UnitTest.Basket.Application _basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny())) .Returns(Task.FromResult(fakeCustomerBasket)); _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); - _serviceBusMock.Setup(x => x.Publish(It.IsAny())); + _serviceBusMock.Setup(x => x.Publish(nameof(UserCheckoutAcceptedIntegrationEvent), It.IsAny(), null)); //Act var basketController = new BasketController( @@ -80,7 +79,6 @@ namespace UnitTest.Basket.Application var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket); //Assert - Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId); } @@ -132,7 +130,7 @@ namespace UnitTest.Basket.Application //Act var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult; - _serviceBusMock.Verify(mock => mock.Publish(It.IsAny()), Times.Once); + _serviceBusMock.Verify(mock => mock.Publish(nameof(UserCheckoutAcceptedIntegrationEvent), It.IsAny(), null), Times.Once); Assert.NotNull(result); } diff --git a/src/Services/Ordering/Ordering.SignalrHub/AutofacModules/ApplicationModule.cs b/src/Services/Ordering/Ordering.SignalrHub/AutofacModules/ApplicationModule.cs index 030d3b8b5..5ab1055ac 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/AutofacModules/ApplicationModule.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/AutofacModules/ApplicationModule.cs @@ -1,11 +1,4 @@ using Autofac; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Ordering.SignalrHub.IntegrationEvents; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; namespace Ordering.SignalrHub.AutofacModules { @@ -22,8 +15,8 @@ namespace Ordering.SignalrHub.AutofacModules protected override void Load(ContainerBuilder builder) { - builder.RegisterAssemblyTypes(typeof(OrderStatusChangedToAwaitingValidationIntegrationEvent).GetTypeInfo().Assembly) - .AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); + // builder.RegisterAssemblyTypes(typeof(OrderStatusChangedToAwaitingValidationIntegrationEvent).GetTypeInfo().Assembly) + // .AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); } } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs index db70566c3..5e8af8bbc 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs @@ -24,9 +24,9 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling //TODO: [CapSubscribe(nameof(OrderStatusChangedToCancelledIntegrationEvent))] public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); await _hubContext.Clients .Group(@event.BuyerName) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs index 94df598ef..8dd1337e0 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs @@ -24,9 +24,9 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling //TODO [CapSubscribe(nameof(OrderStatusChangedToPaidIntegrationEvent))] public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); await _hubContext.Clients .Group(@event.BuyerName) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs index d92e31bbd..b60958277 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs @@ -24,9 +24,9 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling //TODO [CapSubscribe(nameof(OrderStatusChangedToShippedIntegrationEvent))] public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); await _hubContext.Clients .Group(@event.BuyerName)