33
44using System ;
55using System . Threading . Tasks ;
6+ using Microsoft . Extensions . Logging ;
67using Microsoft . ServiceBus . Messaging ;
78
89namespace NuGet . Services . ServiceBus
910{
1011 public class SubscriptionClientWrapper : ISubscriptionClient
1112 {
1213 private readonly SubscriptionClient _client ;
14+ private readonly ILogger < SubscriptionClientWrapper > _logger ;
1315
14- public SubscriptionClientWrapper ( string connectionString , string topicPath , string name )
16+ public SubscriptionClientWrapper ( string connectionString , string topicPath , string name , ILogger < SubscriptionClientWrapper > logger )
1517 {
1618 _client = SubscriptionClient . CreateFromConnectionString ( connectionString , topicPath , name ) ;
19+ _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
1720 }
1821
1922 public void OnMessageAsync ( Func < IBrokeredMessage , Task > onMessageAsync )
2023 {
2124 var callback = CreateOnMessageAsyncCallback ( onMessageAsync ) ;
2225
26+ var onMessageOptions = new OnMessageOptions ( ) ;
27+ onMessageOptions . ExceptionReceived += OnMessageException ;
28+
2329 _client . OnMessageAsync ( callback ) ;
2430 }
2531
@@ -36,9 +42,17 @@ public void OnMessageAsync(Func<IBrokeredMessage, Task> onMessageAsync, IOnMessa
3642 nameof ( options ) ) ;
3743 }
3844
45+ var onMessageOptions = optionsWrapper . GetOptions ( ) ;
46+ onMessageOptions . ExceptionReceived += OnMessageException ;
47+
3948 _client . OnMessageAsync (
4049 CreateOnMessageAsyncCallback ( onMessageAsync ) ,
41- optionsWrapper . OnMessageOptions ) ;
50+ onMessageOptions ) ;
51+ }
52+
53+ private void OnMessageException ( object sender , ExceptionReceivedEventArgs e )
54+ {
55+ _logger . LogError ( 0 , e . Exception , "Unhandled exception while processing ServiceBus message" ) ;
4256 }
4357
4458 private Func < BrokeredMessage , Task > CreateOnMessageAsyncCallback ( Func < IBrokeredMessage , Task > onMessageAsync )
0 commit comments