77using Projections ;
88using Repository ;
99using Shared . DomainDrivenDesign . EventSourcing ;
10+ using Shared . General ;
1011using Shared . Logger ;
1112using State ;
1213
@@ -25,12 +26,10 @@ public ProjectionHandler(IProjectionStateRepository<TState> projectionStateRepos
2526 this . StateDispatcher = stateDispatcher ;
2627 }
2728
28- public async Task Handle ( IDomainEvent @event , CancellationToken cancellationToken )
29- {
29+ public async Task Handle ( IDomainEvent @event , CancellationToken cancellationToken ) {
3030 if ( @event == null ) return ;
31-
32- if ( this . Projection . ShouldIHandleEvent ( @event ) == false )
33- {
31+
32+ if ( this . Projection . ShouldIHandleEvent ( @event ) == false ) {
3433 return ;
3534 }
3635
@@ -40,8 +39,7 @@ public async Task Handle(IDomainEvent @event, CancellationToken cancellationToke
4039 //Load the state from persistence
4140 TState state = await this . ProjectionStateRepository . Load ( @event , cancellationToken ) ;
4241
43- if ( state == null )
44- {
42+ if ( state == null ) {
4543 return ;
4644 }
4745
@@ -50,41 +48,40 @@ public async Task Handle(IDomainEvent @event, CancellationToken cancellationToke
5048 builder . Append ( $ "{ stopwatch . ElapsedMilliseconds } ms Handling { @event . EventType } Id [{ @event . EventId } ] for state { state . GetType ( ) . Name } |") ;
5149
5250 TState newState = await this . Projection . Handle ( state , @event , cancellationToken ) ;
53-
51+
5452 builder . Append ( $ "{ stopwatch . ElapsedMilliseconds } ms After Handle|") ;
5553
56- if ( newState != state )
57- {
58- newState = newState with
59- {
60- ChangesApplied = true
61- } ;
62-
54+ if ( newState != state ) {
55+ newState = newState with {
56+ ChangesApplied = true
57+ } ;
58+
6359 // save state
6460 newState = await this . ProjectionStateRepository . Save ( newState , @event , cancellationToken ) ;
6561
6662 //Repo might have detected a duplicate event
6763 builder . Append ( $ "{ stopwatch . ElapsedMilliseconds } ms After Save|") ;
6864
69- if ( this . StateDispatcher != null )
70- {
65+ if ( this . StateDispatcher != null ) {
7166 // Send to anyone else interested
7267 await this . StateDispatcher . Dispatch ( newState , @event , cancellationToken ) ;
7368
7469 builder . Append ( $ "{ stopwatch . ElapsedMilliseconds } ms After Dispatch|") ;
7570 }
76-
71+
7772 }
78- else
79- {
73+ else {
8074 builder . Append ( $ "{ stopwatch . ElapsedMilliseconds } ms No Save required|") ;
8175 }
8276
8377 stopwatch . Stop ( ) ;
8478
8579 builder . Insert ( 0 , $ "Total time: { stopwatch . ElapsedMilliseconds } ms|") ;
86- Logger . LogWarning ( builder . ToString ( ) ) ;
87- Logger . LogInformation ( $ "Event Type { @event . EventType } Id [{ @event . EventId } ] for state { state . GetType ( ) . Name } took { stopwatch . ElapsedMilliseconds } ms to process") ;
8880
81+ Int32 projectionTraceThresholdInSeconds = Int32 . Parse ( ConfigurationReader . GetValue ( "AppSettings" , "ProjectionTraceThresholdInSeconds" ) ) ;
82+ if ( stopwatch . Elapsed . Seconds > projectionTraceThresholdInSeconds ) {
83+ Logger . LogWarning ( builder . ToString ( ) ) ;
84+ Logger . LogInformation ( $ "Event Type { @event . EventType } Id [{ @event . EventId } ] for state { state . GetType ( ) . Name } took { stopwatch . ElapsedMilliseconds } ms to process") ;
85+ }
8986 }
9087}
0 commit comments