public static IDistributor <T> Trace <T>( this IDistributor <T> distributor, Action <Lease <T> > onLeaseAcquired = null, Action <Lease <T> > onLeaseReleasing = null) { if (distributor == null) { throw new ArgumentNullException("distributor"); } return(Create( start: () => { System.Diagnostics.Trace.WriteLine("[Distribute] Start"); return distributor.Start(); }, onReceive: receive => { onLeaseAcquired = onLeaseAcquired ?? TraceOnLeaseAcquired; onLeaseReleasing = onLeaseReleasing ?? TraceOnLeaseReleasing; // FIX: (Trace) this doesn't do anything if OnReceive was called before Trace, so a proper pipeline model may be better here. distributor.OnReceive(async lease => { onLeaseAcquired(lease); await receive(lease); onLeaseReleasing(lease); }); }, stop: () => { System.Diagnostics.Trace.WriteLine("[Distribute] Stop"); return distributor.Stop(); }, distribute: distributor.Distribute)); }