@@ -117,6 +117,88 @@ public void WriteEvent_LoggingWithoutStrategyKey_Ok()
117117 messages [ 0 ] . Message . Should ( ) . Be ( "Resilience event occurred. EventName: 'my-event', Builder Name: 'my-builder', Strategy Name: 'my-strategy', Strategy Type: 'my-strategy-type', Strategy Key: '(null)', Result: '(null)'" ) ;
118118 }
119119
120+ [ Fact ]
121+ public void WriteExecutionAttempt_LoggingWithException_Ok ( )
122+ {
123+ var telemetry = Create ( ) ;
124+ using var response = new HttpResponseMessage ( System . Net . HttpStatusCode . OK ) ;
125+ ReportEvent ( telemetry , Outcome . FromException < object > ( new InvalidOperationException ( "Dummy message." ) ) , arg : new ExecutionAttemptArguments ( 4 , TimeSpan . FromMilliseconds ( 123 ) , true ) ) ;
126+
127+ var messages = _logger . GetRecords ( new EventId ( 3 , "ExecutionAttempt" ) ) . ToList ( ) ;
128+ messages . Should ( ) . HaveCount ( 1 ) ;
129+
130+ messages [ 0 ] . Message . Should ( ) . Be ( "Execution attempt. Builder Name: 'my-builder', Strategy Name: 'my-strategy', Strategy Type: 'my-strategy-type', Strategy Key: 'my-strategy-key', Result: 'Dummy message.', Handled: 'True', Attempt: '4', Execution Time: '123'" ) ;
131+ }
132+
133+ [ InlineData ( true , true ) ]
134+ [ InlineData ( false , true ) ]
135+ [ InlineData ( true , false ) ]
136+ [ InlineData ( false , false ) ]
137+ [ Theory ]
138+ public void WriteExecutionAttempt_LoggingWithOutcome_Ok ( bool noOutcome , bool handled )
139+ {
140+ var telemetry = Create ( ) ;
141+ using var response = new HttpResponseMessage ( System . Net . HttpStatusCode . OK ) ;
142+ ReportEvent ( telemetry , noOutcome ? null : Outcome . FromResult < object > ( response ) , arg : new ExecutionAttemptArguments ( 4 , TimeSpan . FromMilliseconds ( 123 ) , handled ) ) ;
143+
144+ var messages = _logger . GetRecords ( new EventId ( 3 , "ExecutionAttempt" ) ) . ToList ( ) ;
145+ messages . Should ( ) . HaveCount ( 1 ) ;
146+
147+ if ( noOutcome )
148+ {
149+ #if NET6_0_OR_GREATER
150+ string resultString = string . Empty ;
151+ #else
152+ string resultString = "(null)" ;
153+ #endif
154+
155+ messages [ 0 ] . Message . Should ( ) . Be ( $ "Execution attempt. Builder Name: 'my-builder', Strategy Name: 'my-strategy', Strategy Type: 'my-strategy-type', Strategy Key: 'my-strategy-key', Result: '{ resultString } ', Handled: '{ handled } ', Attempt: '4', Execution Time: '123'") ;
156+ }
157+ else
158+ {
159+ messages [ 0 ] . Message . Should ( ) . Be ( $ "Execution attempt. Builder Name: 'my-builder', Strategy Name: 'my-strategy', Strategy Type: 'my-strategy-type', Strategy Key: 'my-strategy-key', Result: '200', Handled: '{ handled } ', Attempt: '4', Execution Time: '123'") ;
160+ }
161+
162+ if ( handled )
163+ {
164+ messages [ 0 ] . LogLevel . Should ( ) . Be ( LogLevel . Warning ) ;
165+ }
166+ else
167+ {
168+ messages [ 0 ] . LogLevel . Should ( ) . Be ( LogLevel . Debug ) ;
169+ }
170+
171+ #if NET6_0_OR_GREATER
172+ // verify reported state
173+ var coll = messages [ 0 ] . State . Should ( ) . BeAssignableTo < IReadOnlyList < KeyValuePair < string , object > > > ( ) . Subject ;
174+ coll . Count . Should ( ) . Be ( 9 ) ;
175+ coll . AsEnumerable ( ) . Should ( ) . HaveCount ( 9 ) ;
176+ ( coll as IEnumerable ) . GetEnumerator ( ) . Should ( ) . NotBeNull ( ) ;
177+
178+ for ( int i = 0 ; i < coll . Count ; i ++ )
179+ {
180+ if ( ! noOutcome )
181+ {
182+ coll [ i ] . Value . Should ( ) . NotBeNull ( ) ;
183+ }
184+ }
185+
186+ coll . Invoking ( c => c [ coll . Count + 1 ] ) . Should ( ) . Throw < IndexOutOfRangeException > ( ) ;
187+ #endif
188+ }
189+
190+ [ Fact ]
191+ public void WriteExecutionAttempt_NotEnabled_EnsureNotLogged ( )
192+ {
193+ var telemetry = Create ( ) ;
194+ _logger . Enabled = false ;
195+
196+ ReportEvent ( telemetry , null , arg : new ExecutionAttemptArguments ( 4 , TimeSpan . FromMilliseconds ( 123 ) , true ) ) ;
197+
198+ var messages = _logger . GetRecords ( new EventId ( 3 , "ExecutionAttempt" ) ) . ToList ( ) ;
199+ messages . Should ( ) . HaveCount ( 0 ) ;
200+ }
201+
120202 [ InlineData ( true , false ) ]
121203 [ InlineData ( false , false ) ]
122204 [ InlineData ( true , true ) ]
0 commit comments