@@ -120,6 +120,21 @@ public class EventHubTarget : AsyncTaskTarget
120120 /// </summary>
121121 public Layout AccessKey { get ; set ; }
122122
123+ /// <summary>
124+ /// The connection uses the AMQP protocol over web sockets. See also <see cref="EventHubsTransportType.AmqpWebSockets"/>
125+ /// </summary>
126+ public Layout UseWebSockets { get ; set ; }
127+
128+ /// <summary>
129+ /// The proxy to use for communication over web sockets.
130+ /// </summary>
131+ public Layout WebSocketProxyAddress { get ; set ; }
132+
133+ /// <summary>
134+ /// Custom endpoint address that can be used when establishing the connection.
135+ /// </summary>
136+ public Layout CustomEndpointAddress { get ; set ; }
137+
123138 /// <summary>
124139 /// Gets a list of user properties (aka custom properties) to add to the AMQP message
125140 /// </summary>
@@ -164,6 +179,9 @@ protected override void InitializeTarget()
164179 string storageAccountName = string . Empty ;
165180 string storageAccountAccessKey = string . Empty ;
166181 string eventHubName = string . Empty ;
182+ string useWebSockets = string . Empty ;
183+ string webSocketProxyAddress = string . Empty ;
184+ string customEndPointAddress = string . Empty ;
167185
168186 var defaultLogEvent = LogEventInfo . CreateNullEvent ( ) ;
169187
@@ -182,7 +200,15 @@ protected override void InitializeTarget()
182200 storageAccountAccessKey = AccessKey ? . Render ( defaultLogEvent ) ;
183201 }
184202
185- _eventHubService . Connect ( connectionString , eventHubName , serviceUri , tenantIdentity , resourceIdentifier , clientIdentity , sharedAccessSignature , storageAccountName , storageAccountAccessKey ) ;
203+ useWebSockets = UseWebSockets ? . Render ( defaultLogEvent ) ?? string . Empty ;
204+ if ( ! string . IsNullOrEmpty ( useWebSockets ) && ( string . Equals ( useWebSockets . Trim ( ) , bool . TrueString , StringComparison . OrdinalIgnoreCase ) || string . Equals ( useWebSockets . Trim ( ) , "1" , StringComparison . OrdinalIgnoreCase ) ) )
205+ {
206+ useWebSockets = bool . TrueString ;
207+ }
208+ customEndPointAddress = CustomEndpointAddress ? . Render ( defaultLogEvent ) ?? string . Empty ;
209+ webSocketProxyAddress = WebSocketProxyAddress ? . Render ( defaultLogEvent ) ?? string . Empty ;
210+
211+ _eventHubService . Connect ( connectionString , eventHubName , serviceUri , tenantIdentity , resourceIdentifier , clientIdentity , sharedAccessSignature , storageAccountName , storageAccountAccessKey , bool . TrueString == useWebSockets , webSocketProxyAddress , customEndPointAddress ) ;
186212 InternalLogger . Debug ( "AzureEventHubTarget(Name={0}): Initialized" , Name ) ;
187213 }
188214 catch ( Exception ex )
@@ -470,33 +496,42 @@ private sealed class EventHubService : IEventHubService
470496
471497 public string EventHubName { get ; private set ; }
472498
473- public void Connect ( string connectionString , string eventHubName , string serviceUri , string tenantIdentity , string resourceIdentifier , string clientIdentity , string sharedAccessSignature , string storageAccountName , string storageAccountAccessKey )
499+ public void Connect ( string connectionString , string eventHubName , string serviceUri , string tenantIdentity , string resourceIdentifier , string clientIdentity , string sharedAccessSignature , string storageAccountName , string storageAccountAccessKey , bool useWebSockets , string webSocketsProxyAddress , string endPointAddress )
474500 {
475501 EventHubName = eventHubName ;
476502
503+ Azure . Messaging . EventHubs . Producer . EventHubProducerClientOptions options = default ;
504+ if ( useWebSockets || ! string . IsNullOrEmpty ( webSocketsProxyAddress ) || ! string . IsNullOrEmpty ( endPointAddress ) )
505+ {
506+ options = new Azure . Messaging . EventHubs . Producer . EventHubProducerClientOptions ( ) ;
507+ options . ConnectionOptions . TransportType = useWebSockets ? EventHubsTransportType . AmqpWebSockets : options . ConnectionOptions . TransportType ;
508+ options . ConnectionOptions . Proxy = ! string . IsNullOrEmpty ( webSocketsProxyAddress ) ? new System . Net . WebProxy ( webSocketsProxyAddress , true ) : options . ConnectionOptions . Proxy ;
509+ options . ConnectionOptions . CustomEndpointAddress = ! string . IsNullOrEmpty ( endPointAddress ) ? new Uri ( endPointAddress ) : options . ConnectionOptions . CustomEndpointAddress ;
510+ }
511+
477512 if ( string . IsNullOrWhiteSpace ( serviceUri ) )
478513 {
479514 if ( string . IsNullOrWhiteSpace ( eventHubName ) )
480515 {
481- _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( connectionString ) ;
516+ _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( connectionString , options ) ;
482517 }
483518 else
484519 {
485- _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( connectionString , eventHubName ) ;
520+ _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( connectionString , eventHubName , options ) ;
486521 }
487522 }
488523 else if ( ! string . IsNullOrWhiteSpace ( sharedAccessSignature ) )
489524 {
490- _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( serviceUri , eventHubName , new Azure . AzureSasCredential ( sharedAccessSignature ) ) ;
525+ _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( serviceUri , eventHubName , new Azure . AzureSasCredential ( sharedAccessSignature ) , options ) ;
491526 }
492527 else if ( ! string . IsNullOrWhiteSpace ( storageAccountName ) )
493528 {
494- _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( serviceUri , eventHubName , new Azure . AzureNamedKeyCredential ( storageAccountName , storageAccountAccessKey ) ) ;
529+ _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( serviceUri , eventHubName , new Azure . AzureNamedKeyCredential ( storageAccountName , storageAccountAccessKey ) , options ) ;
495530 }
496531 else
497532 {
498533 var tokenCredentials = AzureCredentialHelpers . CreateTokenCredentials ( clientIdentity , tenantIdentity , resourceIdentifier ) ;
499- _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( serviceUri , eventHubName , tokenCredentials ) ;
534+ _client = new Azure . Messaging . EventHubs . Producer . EventHubProducerClient ( serviceUri , eventHubName , tokenCredentials , options ) ;
500535 }
501536 }
502537
0 commit comments