@@ -291,8 +291,8 @@ public void WriteEvent_MeteringWithEnrichers_Ok(int count)
291291
292292 const int DefaultDimensions = 6 ;
293293
294- var telemetry = Create ( new [ ]
295- {
294+ var telemetry = Create (
295+ [
296296 new CallbackEnricher ( context =>
297297 {
298298 for ( int i = 0 ; i < count ; i ++ )
@@ -305,7 +305,7 @@ public void WriteEvent_MeteringWithEnrichers_Ok(int count)
305305 {
306306 context . Tags . Add ( new KeyValuePair < string , object ? > ( "other" , "other-value" ) ) ;
307307 } )
308- } ) ;
308+ ] ) ;
309309
310310 ReportEvent ( telemetry , Outcome . FromResult < object > ( true ) ) ;
311311
@@ -357,16 +357,17 @@ public void PipelineExecution_Logged(bool exception)
357357 var outcome = exception ? Outcome . FromException < object > ( new InvalidOperationException ( "dummy message" ) ) : Outcome . FromResult ( ( object ) 10 ) ;
358358 var result = exception ? "dummy message" : "10" ;
359359
360- ReportEvent ( telemetry , outcome : outcome , arg : default ( PipelineExecutingArguments ) , context : context ) ;
361- ReportEvent ( telemetry , outcome : outcome , arg : new PipelineExecutedArguments ( TimeSpan . FromSeconds ( 10 ) ) , context : context ) ;
360+ ReportEvent ( telemetry , outcome : outcome , arg : default ( PipelineExecutingArguments ) , context : context , severity : ResilienceEventSeverity . Debug ) ;
361+ ReportEvent ( telemetry , outcome : outcome , arg : new PipelineExecutedArguments ( TimeSpan . FromSeconds ( 10 ) ) , context : context , severity : ResilienceEventSeverity . Information ) ;
362362
363363 var messages = _logger . GetRecords ( new EventId ( 1 , "StrategyExecuting" ) ) . ToList ( ) ;
364364 messages . Count . ShouldBe ( 1 ) ;
365365 messages [ 0 ] . Message . ShouldBe ( "Resilience pipeline executing. Source: 'my-pipeline/my-instance', Operation Key: 'op-key'" ) ;
366+ messages [ 0 ] . LogLevel . ShouldBe ( LogLevel . Debug ) ;
366367 messages = [ .. _logger . GetRecords ( new EventId ( 2 , "StrategyExecuted" ) ) ] ;
367368 messages . Count . ShouldBe ( 1 ) ;
368369 messages [ 0 ] . Message . ShouldMatch ( $ "Resilience pipeline executed. Source: 'my-pipeline/my-instance', Operation Key: 'op-key', Result: '{ result } ', Execution Time: 10000ms") ;
369- messages [ 0 ] . LogLevel . ShouldBe ( LogLevel . Debug ) ;
370+ messages [ 0 ] . LogLevel . ShouldBe ( LogLevel . Information ) ;
370371 }
371372
372373 [ Fact ]
@@ -404,8 +405,8 @@ public void PipelineExecution_Metered(bool exception)
404405 var outcome = exception ? Outcome . FromException < object > ( new InvalidOperationException ( "dummy message" ) ) : Outcome . FromResult ( ( object ) 10 ) ;
405406 var result = exception ? "dummy message" : "10" ;
406407
407- var telemetry = Create ( new [ ]
408- {
408+ var telemetry = Create (
409+ [
409410 new CallbackEnricher ( context =>
410411 {
411412 if ( exception )
@@ -415,7 +416,7 @@ public void PipelineExecution_Metered(bool exception)
415416
416417 context . Tags . Add ( new ( "custom-tag" , "custom-tag-value" ) ) ;
417418 } )
418- } ) ;
419+ ] ) ;
419420
420421 ReportEvent ( telemetry , outcome : outcome , arg : new PipelineExecutedArguments ( TimeSpan . FromSeconds ( 10 ) ) , context : context ) ;
421422
@@ -489,6 +490,59 @@ public void SeverityProvider_EnsureRespected()
489490 called . ShouldBeTrue ( ) ;
490491 }
491492
493+ [ Fact ]
494+ public void SeverityProvider_EnsureRespected_For_PipelineEvents ( )
495+ {
496+ var context = ResilienceContextPool . Shared . Get ( "op-key" , TestCancellation . Token ) . WithResultType < int > ( ) ;
497+ var outcome = Outcome . FromException < object > ( new InvalidOperationException ( "dummy message" ) ) ;
498+
499+ var severity = ResilienceEventSeverity . Critical ;
500+ var expectedLogLevel = LogLevel . Critical ;
501+
502+ const string PipelineExecuting = nameof ( PipelineExecuting ) ;
503+ const string PipelineExecuted = nameof ( PipelineExecuted ) ;
504+
505+ var telemetry = Create ( configure : options =>
506+ {
507+ options . SeverityProvider = args =>
508+ {
509+ return args . Event . EventName switch
510+ {
511+ PipelineExecuting => severity ,
512+ PipelineExecuted => severity ,
513+ _ => ResilienceEventSeverity . None
514+ } ;
515+ } ;
516+ } ) ;
517+
518+ ReportEvent ( telemetry , outcome : outcome , arg : default ( PipelineExecutingArguments ) , context : context , eventName : PipelineExecuting ) ;
519+ ReportEvent ( telemetry , outcome : outcome , arg : default ( PipelineExecutedArguments ) , context : context , eventName : PipelineExecuted ) ;
520+
521+ _logger . GetRecords ( new EventId ( 1 , "StrategyExecuting" ) ) . Single ( ) . LogLevel . ShouldBe ( expectedLogLevel ) ;
522+ _logger . GetRecords ( new EventId ( 2 , "StrategyExecuted" ) ) . Single ( ) . LogLevel . ShouldBe ( expectedLogLevel ) ;
523+ }
524+
525+ [ InlineData ( ( ResilienceEventSeverity ) ( - 1 ) ) ]
526+ [ InlineData ( ( ResilienceEventSeverity ) 6 ) ]
527+ [ Theory ]
528+ public void SeverityProvider_EnsureLogLevelChecked ( ResilienceEventSeverity severity )
529+ {
530+ var context = ResilienceContextPool . Shared . Get ( "op-key" , TestCancellation . Token ) . WithResultType < int > ( ) ;
531+ var outcome = Outcome . FromException < object > ( new InvalidOperationException ( "dummy message" ) ) ;
532+ var telemetry = Create ( configure : options => options . SeverityProvider = args => severity ) ;
533+
534+ ReportEvent ( telemetry , outcome : outcome , arg : default ( PipelineExecutingArguments ) , context : context , eventName : "PipelineExecuting" ) ;
535+ ReportEvent ( telemetry , outcome : outcome , arg : default ( ExecutionAttemptArguments ) , context : context , eventName : "ExecutionAttempt" ) ;
536+ ReportEvent ( telemetry , outcome : outcome , arg : default ( Retry . OnRetryArguments < object > ) , context : context , eventName : "OnRetry" ) ;
537+ ReportEvent ( telemetry , outcome : outcome , arg : default ( PipelineExecutedArguments ) , context : context , eventName : "PipelineExecuted" ) ;
538+
539+ var expectedLogLevel = LogLevel . None ;
540+ _logger . GetRecords ( new EventId ( 0 , "ResilienceEvent" ) ) . Single ( ) . LogLevel . ShouldBe ( expectedLogLevel ) ;
541+ _logger . GetRecords ( new EventId ( 1 , "StrategyExecuting" ) ) . Single ( ) . LogLevel . ShouldBe ( expectedLogLevel ) ;
542+ _logger . GetRecords ( new EventId ( 2 , "StrategyExecuted" ) ) . Single ( ) . LogLevel . ShouldBe ( expectedLogLevel ) ;
543+ _logger . GetRecords ( new EventId ( 3 , "ExecutionAttempt" ) ) . Single ( ) . LogLevel . ShouldBe ( expectedLogLevel ) ;
544+ }
545+
492546 private List < Dictionary < string , object ? > > GetEvents ( string eventName ) => [ .. _events . Where ( e => e . Name == eventName ) . Select ( v => v . Tags ) ] ;
493547
494548 private TelemetryListenerImpl Create ( IEnumerable < MeteringEnricher > ? enrichers = null , Action < TelemetryOptions > ? configure = null )
@@ -522,19 +576,22 @@ private static void ReportEvent(
522576 string ? instanceName = "my-instance" ,
523577 ResilienceContext ? context = null ,
524578 TestArguments ? arg = null ,
525- ResilienceEventSeverity severity = ResilienceEventSeverity . Warning ) => ReportEvent < TestArguments > ( telemetry , outcome , instanceName , context , arg ! , severity ) ;
579+ ResilienceEventSeverity severity = ResilienceEventSeverity . Warning ,
580+ string eventName = "my-event" )
581+ => ReportEvent < TestArguments > ( telemetry , outcome , instanceName , context , arg ! , severity , eventName ) ;
526582
527583 private static void ReportEvent < TArgs > (
528584 TelemetryListenerImpl telemetry ,
529585 Outcome < object > ? outcome ,
530586 string ? instanceName = "my-instance" ,
531587 ResilienceContext ? context = null ,
532588 TArgs arg = default ! ,
533- ResilienceEventSeverity severity = ResilienceEventSeverity . Warning ) =>
589+ ResilienceEventSeverity severity = ResilienceEventSeverity . Warning ,
590+ string eventName = "my-event" ) =>
534591 telemetry . Write (
535592 new TelemetryEventArguments < object , TArgs > (
536593 new ResilienceTelemetrySource ( "my-pipeline" , instanceName , "my-strategy" ) ,
537- new ResilienceEvent ( severity , "my-event" ) ,
594+ new ResilienceEvent ( severity , eventName ) ,
538595 context ?? ResilienceContextPool . Shared . Get ( "op-key" ) ,
539596 arg ! ,
540597 outcome ) ) ;
0 commit comments