diff --git a/src/Elders.Cronus/EventStore/LoggingAggregateRepository.cs b/src/Elders.Cronus/EventStore/LoggingAggregateRepository.cs index dc524456..3cb59a15 100644 --- a/src/Elders.Cronus/EventStore/LoggingAggregateRepository.cs +++ b/src/Elders.Cronus/EventStore/LoggingAggregateRepository.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using System; using System.Threading.Tasks; namespace Elders.Cronus.EventStore @@ -29,11 +30,26 @@ public async Task SaveAsync(AR aggregateRoot) where AR : IAggregateRoot { using (logger.BeginScope(s => s .AddScope(Log.AggregateName, typeof(AR).Name) - .AddScope(Log.AggregateId, aggregateRoot.State.Id.Value))) + .AddScope(Log.AggregateId, GetAggregateRootId(aggregateRoot)))) { await realDeal.SaveAsync(aggregateRoot).ConfigureAwait(false); logger.Debug(() => "Aggregate has been saved."); } } + + #region Welcome + private string GetAggregateRootId(AR aggregateRoot) where AR : IAggregateRoot + { + string aggregateId = string.Empty; + try { aggregateId = aggregateRoot.State.Id.Value; } + catch (Exception ex) + { + logger.ErrorException(ex, () => $"Unable to save aggregate. There is a problem with the ID for {typeof(AR).Name}"); + throw new Exception($"Unable to save aggregate. There is a problem with the ID for {typeof(AR).Name}. Check the inner exception if you want to be confused even more.", ex); + } + + return aggregateId; + } + #endregion } }