@@ -205,6 +205,114 @@ await client.ExecuteAsync(
205205 Assert . Empty ( stoppedActivities ) ;
206206 }
207207 }
208+
209+ [ Theory ]
210+ [ InlineData ( true , true , true ) ]
211+ [ InlineData ( true , true , false ) ]
212+ [ InlineData ( true , false , true ) ]
213+ [ InlineData ( false , true , true ) ]
214+ [ InlineData ( true , false , false ) ]
215+ [ InlineData ( false , false , true ) ]
216+ [ InlineData ( false , true , false ) ]
217+ [ InlineData ( false , false , false ) ]
218+ public async Task RecordExceptionTest (
219+ bool recordException ,
220+ bool triggerException ,
221+ bool runAsync )
222+ {
223+ List < Activity > stoppedActivities = [ ] ;
224+ List < Activity > startedActivities = [ ] ;
225+
226+ List < Exception > recordedExceptions = [ ] ;
227+ using var activityListener = new ActivityListener
228+ {
229+ ShouldListenTo = activitySource => true ,
230+ ActivityStarted = startedActivities . Add ,
231+ ActivityStopped = stoppedActivities . Add ,
232+ } ;
233+
234+ activityListener . ExceptionRecorder += ( Activity activity , Exception ex , ref TagList tags ) =>
235+ {
236+ recordedExceptions . Add ( ex ) ;
237+ } ;
238+
239+ ActivitySource . AddActivityListener ( activityListener ) ;
240+
241+ var tracerProvider = Sdk . CreateTracerProviderBuilder ( )
242+ . AddWcfInstrumentation ( options =>
243+ {
244+ options . RecordException = recordException ;
245+ } )
246+ . Build ( ) ;
247+
248+ var client = new ServiceClient (
249+ new NetTcpBinding ( ) ,
250+ new EndpointAddress ( new Uri ( this . serviceBaseUri , "/Service" ) ) ) ;
251+ try
252+ {
253+ if ( triggerException )
254+ {
255+ if ( runAsync )
256+ {
257+ await client . ErrorAsync ( ) ;
258+ }
259+ else
260+ {
261+ client . ErrorSynchronous ( ) ;
262+ }
263+ }
264+ else
265+ {
266+ if ( runAsync )
267+ {
268+ await client . ExecuteAsync (
269+ new ServiceRequest (
270+ payload : "Hello Open Telemetry!" ) ) ;
271+ }
272+ else
273+ {
274+ client . ExecuteSynchronous (
275+ new ServiceRequest (
276+ payload : "Hello Open Telemetry!" ) ) ;
277+ }
278+ }
279+ }
280+ catch ( Exception )
281+ {
282+ }
283+ finally
284+ {
285+ startedActivities [ 0 ] . AddTag ( nameof ( recordException ) , recordException ) ;
286+ startedActivities [ 0 ] . AddTag ( nameof ( triggerException ) , triggerException ) ;
287+ startedActivities [ 0 ] . AddTag ( nameof ( runAsync ) , runAsync ) ;
288+
289+ if ( client . State == CommunicationState . Faulted )
290+ {
291+ client . Abort ( ) ;
292+ }
293+ else
294+ {
295+ client . Close ( ) ;
296+ }
297+
298+ tracerProvider ? . Shutdown ( ) ;
299+ tracerProvider ? . Dispose ( ) ;
300+
301+ WcfInstrumentationActivitySource . Options = null ;
302+ }
303+
304+ Assert . NotEmpty ( stoppedActivities ) ;
305+ var activity = Assert . Single ( stoppedActivities ) ;
306+
307+ if ( recordException && triggerException )
308+ {
309+ Assert . Collection ( recordedExceptions , e => Assert . IsType < Exception > ( e ) ) ;
310+ }
311+ else
312+ {
313+ Assert . Empty ( recordedExceptions ) ;
314+ }
315+ }
208316}
209317
210318#endif
0 commit comments