Skip to content

Commit

Permalink
Logging with CorrelationId added
Browse files Browse the repository at this point in the history
  • Loading branch information
ebubekirdinc committed Mar 20, 2023
1 parent 09a3658 commit c379417
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ Development is still in progress.

[Getting Started](https://github.com/ebubekirdinc/SuuCat/wiki/GettingStarted)

[Saga Orchestration](https://github.com/ebubekirdinc/SuuCat/wiki/SagaOrchestration)
[Saga Orchestration with MassTransit and RabbitMq](https://github.com/ebubekirdinc/SuuCat/wiki/SagaOrchestration)

[Eventual Consistency](https://github.com/ebubekirdinc/SuuCat/wiki/EventualConsistency)

[Health Check](https://github.com/ebubekirdinc/SuuCat/wiki/HealthCheck)
[Distributed Logging with ElasticSearch, Kibana and SeriLog](https://github.com/ebubekirdinc/SuuCat/wiki/DistributedLogging)

[Health Check with WatchDogs](https://github.com/ebubekirdinc/SuuCat/wiki/HealthCheck)

## It will have the following concepts. ##
- API Gateway (Ocelot) with BFF pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public async Task Consume(ConsumeContext<ICompletePaymentMessage> context)
// todo payment from stripe service
var paymentSuccess = DateTime.UtcNow.Second % 2 == 0;

if (paymentSuccess)
{
// if (paymentSuccess)
// {
_logger.LogInformation("Payment successfull. {MessageTotalPrice}$ was withdrawn from user with Id= {MessageCustomerId} and correlation Id={MessageCorrelationId}",
context.Message.TotalPrice, context.Message.CustomerId, context.Message.CorrelationId);

Expand All @@ -33,16 +33,16 @@ await _publishEndpoint.Publish(new PaymentCompletedEvent
});

return;
}

_logger.LogInformation("Payment failed. {MessageTotalPrice}$ was not withdrawn from user with Id={MessageCustomerId} and correlation Id={MessageCorrelationId}",
context.Message.TotalPrice, context.Message.CustomerId, context.Message.CorrelationId);

await _publishEndpoint.Publish(new PaymentFailedEvent
{
CorrelationId = context.Message.CorrelationId,
ErrorMessage = "Payment failed",
OrderItemList = context.Message.OrderItemList
});
// }
//
// _logger.LogInformation("Payment failed. {MessageTotalPrice}$ was not withdrawn from user with Id={MessageCustomerId} and correlation Id={MessageCorrelationId}",
// context.Message.TotalPrice, context.Message.CustomerId, context.Message.CorrelationId);
//
// await _publishEndpoint.Publish(new PaymentFailedEvent
// {
// CorrelationId = context.Message.CorrelationId,
// ErrorMessage = "Payment failed",
// OrderItemList = context.Message.OrderItemList
// });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public OrderCreatedEventConsumer(ApplicationDbContext context, ILogger<OrderCrea

public async Task Consume(ConsumeContext<IOrderCreatedEvent> context)
{
var isThereEnoughStock = _context.Stocks.Where(x=>context.Message.OrderItemList.Select(y => y.ProductId).Contains(x.ProductId))
.AsEnumerable()
.All(x =>
context.Message.OrderItemList.Select(y => y.ProductId).Contains(x.ProductId)
&& x.Count > context.Message.OrderItemList.FirstOrDefault(y => y.ProductId == x.ProductId).Count);
var isThereEnoughStock = true;
foreach (var item in _context.Stocks.Where(x => context.Message.OrderItemList.Select(y => y.ProductId).Contains(x.ProductId)).AsEnumerable())
{
if (!context.Message.OrderItemList.Select(y => y.ProductId).Contains(item.ProductId) || item.Count <= context.Message.OrderItemList.FirstOrDefault(y => y.ProductId == item.ProductId).Count)
{
isThereEnoughStock = false;
break;
}
}

if (!isThereEnoughStock)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using EventBus.Messages.Interfaces;
using MassTransit;
using SagaOrchestrationStateMachine.StateInstances;
using ILogger = Serilog.ILogger;

namespace SagaOrchestrationStateMachine.StateMachines;

public class OrderStateMachine : MassTransitStateMachine<OrderStateInstance>
{
private readonly ILogger<OrderStateMachine> _logger;
private readonly ILogger _logger;

// Commands
private Event<ICreateOrderMessage> CreateOrderMessage { get; set; }
Expand All @@ -28,10 +29,9 @@ public class OrderStateMachine : MassTransitStateMachine<OrderStateInstance>
private State PaymentCompleted { get; set; }
private State PaymentFailed { get; set; }

public OrderStateMachine(ILogger<OrderStateMachine> logger)
public OrderStateMachine() //ILogger<OrderStateMachine> logger)
{
_logger = logger;

_logger = Serilog.Log.Logger;
InstanceState(x => x.CurrentState);

Event(() => CreateOrderMessage, y => y.CorrelateBy<int>(x => x.OrderId, z => z.Message.OrderId)
Expand All @@ -42,7 +42,7 @@ public OrderStateMachine(ILogger<OrderStateMachine> logger)

Initially(
When(CreateOrderMessage)
.Then(context => { _logger.LogInformation($"CreateOrderMessage received in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("CreateOrderMessage received in OrderStateMachine: {ContextSaga} ", context.Saga); })
.Then(context =>
{
context.Saga.CustomerId = context.Message.CustomerId;
Expand All @@ -58,11 +58,11 @@ public OrderStateMachine(ILogger<OrderStateMachine> logger)
OrderItemList = context.Message.OrderItemList
})
.TransitionTo(OrderCreated)
.Then(context => { _logger.LogInformation($"OrderCreatedEvent published in OrderStateMachine: {context.Saga} "); }));
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("OrderCreatedEvent published in OrderStateMachine: {ContextSaga} ", context.Saga); }));

During(OrderCreated,
When(StockReservedEvent)
.Then(context => { _logger.LogInformation($"StockReservedEvent received in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("StockReservedEvent received in OrderStateMachine: {ContextSaga} ", context.Saga); })
.TransitionTo(StockReserved)
.Send(new Uri($"queue:{QueuesConsts.CompletePaymentMessageQueueName}"),
context => new CompletePaymentMessage
Expand All @@ -72,9 +72,9 @@ public OrderStateMachine(ILogger<OrderStateMachine> logger)
CustomerId = context.Saga.CustomerId,
OrderItemList = context.Message.OrderItemList
})
.Then(context => { _logger.LogInformation($"CompletePaymentMessage sent in OrderStateMachine: {context.Saga} "); }),
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("CompletePaymentMessage sent in OrderStateMachine: {ContextSaga} ", context.Saga); }),
When(StockReservationFailedEvent)
.Then(context => { _logger.LogInformation($"StockReservationFailedEvent received in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("StockReservationFailedEvent received in OrderStateMachine: {ContextSaga} ", context.Saga); })
.TransitionTo(StockReservationFailed)
.Publish(
context => new OrderFailedEvent
Expand All @@ -83,36 +83,36 @@ public OrderStateMachine(ILogger<OrderStateMachine> logger)
CustomerId = context.Saga.CustomerId,
ErrorMessage = context.Message.ErrorMessage
})
.Then(context => { _logger.LogInformation($"OrderFailedEvent published in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("OrderFailedEvent published in OrderStateMachine: {ContextSaga} ", context.Saga); })
);

During(StockReserved,
When(PaymentCompletedEvent)
.Then(context => { _logger.LogInformation($"PaymentCompletedEvent received in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("PaymentCompletedEvent received in OrderStateMachine: {ContextSaga} ", context.Saga); })
.TransitionTo(PaymentCompleted)
.Publish(
context => new OrderCompletedEvent
{
OrderId = context.Saga.OrderId,
CustomerId = context.Saga.CustomerId
})
.Then(context => { _logger.LogInformation($"OrderCompletedEvent published in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("OrderCompletedEvent published in OrderStateMachine: {ContextSaga} ", context.Saga); })
.Finalize(),
When(PaymentFailedEvent)
.Then(context => { _logger.LogInformation($"PaymentFailedEvent received in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("PaymentFailedEvent received in OrderStateMachine: {ContextSaga} ", context.Saga); })
.Publish(context => new OrderFailedEvent
{
OrderId = context.Saga.OrderId,
CustomerId = context.Saga.CustomerId,
ErrorMessage = context.Message.ErrorMessage
})
.Then(context => { _logger.LogInformation($"OrderFailedEvent published in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("OrderFailedEvent published in OrderStateMachine: {ContextSaga} ", context.Saga); })
.Send(new Uri($"queue:{QueuesConsts.StockRollBackMessageQueueName}"),
context => new StockRollbackMessage
{
OrderItemList = context.Message.OrderItemList
})
.Then(context => { _logger.LogInformation($"StockRollbackMessage sent in OrderStateMachine: {context.Saga} "); })
.Then(context => { _logger.ForContext("CorrelationId", context.Saga.CorrelationId).Information("StockRollbackMessage sent in OrderStateMachine: {ContextSaga} ", context.Saga); })
.TransitionTo(PaymentFailed)
);
}
Expand Down

0 comments on commit c379417

Please sign in to comment.