internal void DispatchEvent(DomainEvent evt) { var chkpoint = evt.CheckpointToken; if (chkpoint > LastCheckpointDispatched) { if (_logger.IsDebugEnabled) { _logger.DebugFormat("Discharded event {0} commit {1} because last checkpoint dispatched for slot {2} is {3}.", evt.CommitId, evt.CheckpointToken, SlotName, _maxCheckpointDispatched); } return; } Interlocked.Increment(ref RebuildProjectionMetrics.CountOfConcurrentDispatchingCommit); //Console.WriteLine("[{0:00}] - Slot {1}", Thread.CurrentThread.ManagedThreadId, slotName); if (_logger.IsDebugEnabled) { _logger.ThreadProperties["commit"] = evt.CommitId; _logger.DebugFormat("Dispatching checkpoit {0} on tenant {1}", evt.CheckpointToken, _config.TenantId); } TenantContext.Enter(_config.TenantId); try { if (_logger.IsDebugEnabled) { _logger.ThreadProperties["evType"] = evt.GetType().Name; _logger.ThreadProperties["evMsId"] = evt.MessageId; _logger.ThreadProperties["evCheckpointToken"] = evt.CheckpointToken; } string eventName = evt.GetType().Name; foreach (var projection in _projections) { var cname = projection.GetCommonName(); if (_logger.IsDebugEnabled) { _logger.ThreadProperties["prj"] = cname; } bool handled; long ticks = 0; try { //pay attention, stopwatch consumes time. var sw = new Stopwatch(); sw.Start(); handled = projection.Handle(evt, true); sw.Stop(); ticks = sw.ElapsedTicks; MetricsHelper.IncrementProjectionCounterRebuild(cname, SlotName, eventName, ticks); } catch (Exception ex) { _logger.FatalFormat(ex, "[Slot: {3} Projection: {4}] Failed checkpoint: {0} StreamId: {1} Event Name: {2}", evt.CheckpointToken, evt.AggregateId, eventName, SlotName, cname ); throw; } _metrics.Inc(cname, eventName, ticks); if (_logger.IsDebugEnabled) { _logger.DebugFormat("[{3}] [{4}] Handled checkpoint {0}: {1} > {2}", evt.CheckpointToken, evt.AggregateId, eventName, SlotName, cname ); } if (_logger.IsDebugEnabled) { _logger.ThreadProperties["prj"] = null; } } ClearLoggerThreadPropertiesForEventDispatchLoop(); } catch (Exception ex) { _logger.ErrorFormat(ex, "Error dispathing commit id: {0}\nMessage: {1}\nError: {2}", evt.CheckpointToken, evt, ex.Message); ClearLoggerThreadPropertiesForEventDispatchLoop(); throw; } _lastCheckpointRebuilded = chkpoint; if (_logger.IsDebugEnabled) { _logger.ThreadProperties["commit"] = null; } Interlocked.Decrement(ref RebuildProjectionMetrics.CountOfConcurrentDispatchingCommit); }