Skip to content

Commit

Permalink
Use record's ToString to improve event logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed May 19, 2024
1 parent 84b4205 commit 7687e18
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/Admin/NCafe.Admin.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
"Microsoft.AspNetCore": "Information",
"NCafe.Infrastructure": "Trace"
}
}
}
2 changes: 1 addition & 1 deletion src/Admin/NCafe.Admin.Domain/Events/ProductCreated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NCafe.Admin.Domain.Events;

public sealed class ProductCreated : Event
public sealed record ProductCreated : Event
{
public ProductCreated(Guid id, string name, decimal price)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Barista/NCafe.Barista.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
"Microsoft.AspNetCore": "Information",
"NCafe.Infrastructure": "Trace"
}
}
}
6 changes: 3 additions & 3 deletions src/Barista/NCafe.Barista.Domain/Events/OrderPlaced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace NCafe.Barista.Domain.Events;

public sealed class OrderPlaced : Event
public sealed record OrderPlaced : Event
{
public OrderPlaced(Guid id)
{
Id = id;
}

public Guid ProductId { get; set; }
public int Quantity { get; set; }
public Guid ProductId { get; init; }
public int Quantity { get; init; }
}
2 changes: 1 addition & 1 deletion src/Barista/NCafe.Barista.Domain/Events/OrderPrepared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NCafe.Barista.Domain.Events;

public sealed class OrderPrepared : Event
public sealed record OrderPrepared : Event
{
public OrderPrepared(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NCafe.Cashier.Api.Projections;

public class ProductCreated : IEvent
public record ProductCreated : IEvent
{
public Guid Id { get; init; }
public string Name { get; init; }
Expand Down
3 changes: 2 additions & 1 deletion src/Cashier/NCafe.Cashier.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
"Microsoft.AspNetCore": "Information",
"NCafe.Infrastructure": "Trace"
}
}
}
2 changes: 1 addition & 1 deletion src/Cashier/NCafe.Cashier.Domain/Events/OrderPaidFor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NCafe.Cashier.Domain.Events;

public sealed class OrderPaidFor : Event
public sealed record OrderPaidFor : Event
{
public OrderPaidFor(Guid id)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Cashier/NCafe.Cashier.Domain/Events/OrderPlaced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace NCafe.Cashier.Domain.Events;

public sealed class OrderPlaced : Event
public sealed record OrderPlaced : Event
{
public OrderPlaced(Guid id)
{
Id = id;
}

public Guid ProductId { get; set; }
public int Quantity { get; set; }
public Guid ProductId { get; init; }
public int Quantity { get; init; }
}
2 changes: 1 addition & 1 deletion src/Common/NCafe.Core/Domain/Event.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NCafe.Core.Domain;

public abstract class Event : IEvent
public abstract record Event : IEvent
{
public Guid Id { get; protected set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using EventStore.Client;
using Microsoft.Extensions.Logging;
using NCafe.Core.Domain;
using NCafe.Core.Repositories;

namespace NCafe.Infrastructure.EventStore;

internal class EventStoreRepository(EventStoreClient eventStoreClient) : IRepository
internal class EventStoreRepository(EventStoreClient eventStoreClient, ILogger<EventStoreRepository> logger) : IRepository
{
private readonly EventStoreClient _eventStoreClient = eventStoreClient;
private readonly ILogger _logger = logger;

public async Task<TAggregate> GetById<TAggregate>(Guid id) where TAggregate : AggregateRoot
{
_logger.LogInformation("Loading aggregate {AggregateType} with ID {AggregateId}", typeof(TAggregate).Name, id);

var streamName = ToStreamName(id, typeof(TAggregate));

var aggregate = (TAggregate)Activator.CreateInstance(typeof(TAggregate), nonPublic: true);
Expand All @@ -20,19 +24,26 @@ public async Task<TAggregate> GetById<TAggregate>(Guid id) where TAggregate : Ag
StreamPosition.Start);

var events = await result.ToListAsync();
_logger.LogTrace("Applying {EventCount} events for aggregate {AggregateType} with ID {AggregateId}",
events.Count, typeof(TAggregate).Name, id);

foreach (var @event in events)
{
var state = @event.AsAggregateEvent();

aggregate.ApplyEvent(state);

_logger.LogTrace("Applied event {EventType} to aggregate {AggregateType} with ID {AggregateId}. Event: {@Event}",
state.GetType().Name, typeof(TAggregate).Name, id, state);
}

return aggregate;
}

public async Task Save(AggregateRoot aggregate)
{
_logger.LogInformation("Saving aggregate {AggregateType} with ID {AggregateId}", aggregate.GetType().Name, aggregate.Id);

var streamName = ToStreamName(aggregate.Id, aggregate.GetType());
var pendingEvents = aggregate.GetPendingEvents().ToArray();

Expand All @@ -43,11 +54,17 @@ public async Task Save(AggregateRoot aggregate)
.Select(e => e.AsEventData())
.ToArray();

_logger.LogTrace("Appending {EventCount} events to aggregate {AggregateType} with ID {AggregateId}. Events: {@Events}.",
eventsToAppend.Length, aggregate.GetType().Name, aggregate.Id, pendingEvents);

var result = await _eventStoreClient.AppendToStreamAsync(
streamName,
StreamRevision.FromInt64(expectedVersion),
eventsToAppend);

_logger.LogInformation("Saved aggregate {AggregateType} with ID {AggregateId}. Version: {Version}",
aggregate.GetType().Name, aggregate.Id, result.NextExpectedStreamRevision.ToInt64());

aggregate.ClearPendingEvents();
}

Expand Down

0 comments on commit 7687e18

Please sign in to comment.