Skip to content

Commit a2b2617

Browse files
committed
EventHub + ServiceHub with EventProducerIdentifier
1 parent 69bb3fc commit a2b2617

File tree

22 files changed

+266
-95
lines changed

22 files changed

+266
-95
lines changed

src/NLog.Extensions.AzureBlobStorage/BlobStorageTarget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ protected override Task WriteAsyncTask(IList<LogEventInfo> logEvents, Cancellati
348348
}
349349
}
350350

351-
return Task.WhenAll(multipleTasks ?? new Task[0]);
351+
return multipleTasks?.Count > 0 ? Task.WhenAll(multipleTasks) : Task.CompletedTask;
352352
}
353353

354354
private byte[] CreateBlobPayload(IList<LogEventInfo> logEvents)
@@ -474,7 +474,7 @@ public override int GetHashCode()
474474
}
475475
}
476476

477-
class CloudBlobService : ICloudBlobService
477+
private sealed class CloudBlobService : ICloudBlobService
478478
{
479479
private IDictionary<string, string> _blobMetadata;
480480
private IDictionary<string, string> _blobTags;

src/NLog.Extensions.AzureBlobStorage/NLog.Extensions.AzureBlobStorage.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
<RepositoryUrl>https://github.com/JDetmar/NLog.Extensions.AzureStorage.git</RepositoryUrl>
2525
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2626
<PackageReleaseNotes>
27-
- Updated Azure.Identity ver. 1.14.2 to fix security issue
28-
- Updated NLog ver. 5.2.5 to support build-triming
29-
- Updated Azure.Storage.Blobs ver. 12.24.1
27+
- Added support for Proxy-options: ProxyAddress, ProxyLogin, ProxyPassword, NoProxy, UseDefaultCredentialsForProxy
3028

3129
Docs: https://github.com/JDetmar/NLog.Extensions.AzureStorage/blob/master/src/NLog.Extensions.AzureBlobStorage/README.md
3230
</PackageReleaseNotes>

src/NLog.Extensions.AzureBlobStorage/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</targets>
2626
```
2727

28-
### Parameters
28+
### General Options
2929

3030
_name_ - Name of the target.
3131

@@ -37,9 +37,11 @@ _container_ - Azure blob container name. [Layout](https://github.com/NLog/NLog/w
3737

3838
_contentType_ - Azure blob ContentType (Default = text/plain)
3939

40-
_connectionString_ - Azure storage connection string. Ex. `UseDevelopmentStorage=true;`
40+
_connectionString_ - Azure Blob Storage connection string from your storage account. Required unless using `ServiceUri`.
4141

42-
_serviceUri_ - Uri to reference the blob service (e.g. https://{account_name}.blob.core.windows.net). Input for `BlobServiceClient`. Required, when `connectionString` is not configured. Overrides `connectionString` when both are set.
42+
_serviceUri_ - Uri to reference the blob service (e.g. https://{account_name}.blob.core.windows.net). Alternative to ConnectionString, where Managed Identiy is applied from DefaultAzureCredential.
43+
44+
### Authentication Options
4345

4446
_managedIdentityClientId_ - Sets `ManagedIdentityClientId` on `DefaultAzureCredentialOptions`. Requires `serviceUri`.
4547

@@ -57,6 +59,8 @@ _clientAuthId_ - clientId for `ClientSecretCredential` authentication. Requires
5759

5860
_clientAuthSecret_ - clientSecret for `ClientSecretCredential` authentication. Requires `serviceUri`,`tenantIdentity` and `clientAuthId`.
5961

62+
### Proxy Options
63+
6064
_noProxy_ - Bypasses any system proxy and proxy in `ProxyAddress` when set to `true`.
6165

6266
_proxyAddress_ - Address of the proxy server to use (e.g. http://proxyserver:8080).
@@ -65,7 +69,7 @@ _proxyLogin_ - Login to use for the proxy server. Requires `proxyPassword`.
6569

6670
_proxyPassword_ - Password to use for the proxy server. Requires `proxyLogin`.
6771

68-
_useDefaultCredentialsForProxy_ - Uses the default credentials (`System.Net.CredentialCache.DefaultCredentials`) for the proxy server, overriding any values that may have been set in `proxyLogin` and `proxyPassword`.
72+
_useDefaultCredentialsForProxy_ - Uses the default credentials (`System.Net.CredentialCache.DefaultCredentials`) for the proxy server.
6973

7074
### Batching Policy
7175

src/NLog.Extensions.AzureDataTables/DataTablesTarget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ protected override Task WriteAsyncTask(IList<LogEventInfo> logEvents, Cancellati
356356
}
357357
}
358358

359-
return Task.WhenAll(multipleTasks ?? new Task[0]);
359+
return multipleTasks?.Count > 0 ? Task.WhenAll(multipleTasks) : Task.CompletedTask;
360360
}
361361

362362
private async Task WriteMultipleBatchesAsync(IEnumerable<IEnumerable<TableTransactionAction>> batchCollection, string tableName, CancellationToken cancellationToken)
@@ -485,7 +485,7 @@ private static string TryLookupValue(Func<string> lookupFunc, string lookupType)
485485
}
486486
}
487487

488-
class CloudTableService : ICloudTableService
488+
private sealed class CloudTableService : ICloudTableService
489489
{
490490
private TableServiceClient _client;
491491
private TableClient _table;

src/NLog.Extensions.AzureDataTables/NLog.Extensions.AzureDataTables.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
<RepositoryUrl>https://github.com/JDetmar/NLog.Extensions.AzureStorage.git</RepositoryUrl>
2525
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2626
<PackageReleaseNotes>
27-
- Updated Azure.Identity ver. 1.14.2 to fix security issue
28-
- Updated NLog ver. 5.2.5 to support build-triming
29-
- Updated Azure.Data.Tables ver. 12.11.0
27+
- Added support for Proxy-options: ProxyAddress, ProxyLogin, ProxyPassword, NoProxy, UseDefaultCredentialsForProxy
28+
- Added support for Authentication-options: ClientAuthId + ClientAuthSecret
3029

3130
Docs: https://github.com/JDetmar/NLog.Extensions.AzureStorage/blob/master/src/NLog.Extensions.AzureDataTables/README.md
3231
</PackageReleaseNotes>

src/NLog.Extensions.AzureDataTables/README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,26 @@ Supports both Azure Storage Tables and CosmosDB Tables.
2222
logTimeStampFormat="O" />
2323
</targets>
2424
```
25-
### Parameters
25+
### General Options
2626

2727
_name_ - Name of the target.
2828

2929
_layout_ - Text to be rendered. [Layout](https://github.com/NLog/NLog/wiki/Layouts) Required.
3030

31-
_connectionString_ - Azure storage connection string. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
31+
_connectionString_ - Azure storage connection string. [Layout](https://github.com/NLog/NLog/wiki/Layouts). Required unless using `serviceUri`.
3232

3333
_serviceUri_ - Alternative to ConnectionString, where Managed Identiy is acquired from DefaultAzureCredential.
3434

35+
_tableName_ - Azure table name. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
36+
37+
_rowKey_ - Azure Table RowKey. [Layout](https://github.com/NLog/NLog/wiki/Layouts). Default = "InverseTicks_${guid}"
38+
39+
_partitionKey_ - Azure PartitionKey. [Layout](https://github.com/NLog/NLog/wiki/Layouts). Default = `${logger}`
40+
41+
_logTimeStampFormat_ - Default Log TimeStamp is set to 'O' for [Round-trip](https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#the-round-trip-o-o-format-specifier) format if not specified.
42+
43+
## Authentication Options
44+
3545
_managedIdentityClientId_ - Sets `ManagedIdentityClientId` on `DefaultAzureCredentialOptions`. Requires `serviceUri`
3646

3747
_managedIdentityResourceId_ - resourceId for `ManagedIdentityResourceId` on `DefaultAzureCredentialOptions`, do not use together with `ManagedIdentityClientId`. Requires `serviceUri`.
@@ -48,6 +58,8 @@ _clientAuthId_ - clientId for `ClientSecretCredential` authentication. Requires
4858

4959
_clientAuthSecret_ - clientSecret for `ClientSecretCredential` authentication. Requires `serviceUri`,`tenantIdentity` and `clientAuthId`.
5060

61+
## Proxy Options
62+
5163
_noProxy_ - Bypasses any system proxy and proxy in `ProxyAddress` when set to `true`.
5264

5365
_proxyAddress_ - Address of the proxy server to use (e.g. http://proxyserver:8080).
@@ -56,15 +68,7 @@ _proxyLogin_ - Login to use for the proxy server. Requires `proxyPassword`.
5668

5769
_proxyPassword_ - Password to use for the proxy server. Requires `proxyLogin`.
5870

59-
_useDefaultCredentialsForProxy_ - Uses the default credentials (`System.Net.CredentialCache.DefaultCredentials`) for the proxy server, overriding any values that may have been set in `proxyLogin` and `proxyPassword`.
60-
61-
_tableName_ - Azure table name. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
62-
63-
_rowKey_ - Azure Table RowKey. [Layout](https://github.com/NLog/NLog/wiki/Layouts). Default = "InverseTicks_${guid}"
64-
65-
_partitionKey_ - Azure PartitionKey. [Layout](https://github.com/NLog/NLog/wiki/Layouts). Default = `${logger}`
66-
67-
_logTimeStampFormat_ - Default Log TimeStamp is set to 'O' for [Round-trip](https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#the-round-trip-o-o-format-specifier) format if not specified.
71+
_useDefaultCredentialsForProxy_ - Uses the default credentials (`System.Net.CredentialCache.DefaultCredentials`) for the proxy server.
6872

6973
### Dynamic TableEntity
7074
Instead of using the predefined NLogEntity-properties, then one can specify wanted properties:

src/NLog.Extensions.AzureEventGrid/NLog.Extensions.AzureEventGrid.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
<RepositoryUrl>https://github.com/JDetmar/NLog.Extensions.AzureStorage.git</RepositoryUrl>
2525
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2626
<PackageReleaseNotes>
27-
- Updated Azure.Identity ver. 1.14.2 to fix security issue
28-
- Updated NLog ver. 5.2.5 to support build-triming
29-
- Updated Azure.Messaging.EventGrid ver. 5.0.0
27+
- Added support for Proxy-options: ProxyAddress, ProxyLogin, ProxyPassword, NoProxy, UseDefaultCredentialsForProxy
28+
- Added support for Authentication-options: ClientAuthId + ClientAuthSecret
3029

3130
Docs: https://github.com/JDetmar/NLog.Extensions.AzureStorage/blob/master/src/NLog.Extensions.AzureEventGrid/README.md
3231
</PackageReleaseNotes>

src/NLog.Extensions.AzureEventGrid/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</targets>
2929
```
3030

31-
### Parameters
31+
### General Options
3232

3333
_name_ - Name of the target.
3434

@@ -48,6 +48,8 @@ _dataFormat_ - Format of the data-payload (Binary / Json). Default Binary. `Stri
4848

4949
_dataSchema_ - Schema version of the data-payload. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
5050

51+
### Authentication Options
52+
5153
_managedIdentityClientId_ - Sets `ManagedIdentityClientId` on `DefaultAzureCredentialOptions`. Requires `serviceUri`.
5254

5355
_managedIdentityResourceId_ - resourceId for `ManagedIdentityResourceId` on `DefaultAzureCredentialOptions`, do not use together with `ManagedIdentityClientId`. Requires `serviceUri`.
@@ -62,6 +64,8 @@ _clientAuthId_ - clientId for `ClientSecretCredential` authentication. Requires
6264

6365
_clientAuthSecret_ - clientSecret for `ClientSecretCredential` authentication. Requires `tenantIdentity` and `clientAuthId`.
6466

67+
### Proxy Options
68+
6569
_noProxy_ - Bypasses any system proxy and proxy in `ProxyAddress` when set to `true`.
6670

6771
_proxyAddress_ - Address of the proxy server to use (e.g. http://proxyserver:8080).
@@ -70,8 +74,7 @@ _proxyLogin_ - Login to use for the proxy server. Requires `proxyPassword`.
7074

7175
_proxyPassword_ - Password to use for the proxy server. Requires `proxyLogin`.
7276

73-
_useDefaultCredentialsForProxy_ - Uses the default credentials (`System.Net.CredentialCache.DefaultCredentials`) for the proxy server, overriding any values that may have been set in `proxyLogin` and `proxyPassword`.
74-
Only applies if `noProxy` is not set to `true`.
77+
_useDefaultCredentialsForProxy_ - Uses the default credentials (`System.Net.CredentialCache.DefaultCredentials`) for the proxy server.
7578

7679
### Retry Policy
7780

src/NLog.Extensions.AzureEventHub/EventHubTarget.cs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Azure.Messaging.EventHubs;
88
using NLog.Common;
99
using NLog.Config;
10+
using NLog.Extensions.AzureBlobStorage;
1011
using NLog.Extensions.AzureStorage;
1112
using NLog.Layouts;
1213

@@ -161,13 +162,50 @@ public class EventHubTarget : AsyncTaskTarget
161162
/// <summary>
162163
/// The proxy to use for communication over web sockets.
163164
/// </summary>
164-
public Layout WebSocketProxyAddress { get; set; }
165+
[Obsolete("Replaced by ProxyAddress to align with other NLog Targets for Azure")]
166+
public Layout WebSocketProxyAddress { get => ProxyAddress; set => ProxyAddress = value; }
165167

166168
/// <summary>
167169
/// Custom endpoint address that can be used when establishing the connection.
168170
/// </summary>
169171
public Layout CustomEndpointAddress { get; set; }
170172

173+
/// <summary>
174+
/// A unique name used to identify the producer. If <c>null</c> or empty, a GUID will be used as the identifier.
175+
/// </summary>
176+
public Layout EventProducerIdentifier { get; set; }
177+
178+
/// <summary>
179+
/// Bypasses any system proxy and proxy in <see cref="ProxyAddress"/> when set to <see langword="true"/>.
180+
/// Overrides <see cref="ProxyAddress"/>.
181+
/// </summary>
182+
/// <remarks>Only applies when <see cref="UseWebSockets"/> = <see langword="true"/></remarks>
183+
public bool NoProxy { get; set; }
184+
185+
/// <summary>
186+
/// Address of the proxy server to use (e.g. http://proxyserver:8080).
187+
/// </summary>
188+
/// <remarks>Only applies when <see cref="UseWebSockets"/> = <see langword="true"/></remarks>
189+
public Layout ProxyAddress { get; set; }
190+
191+
/// <summary>
192+
/// Login to use for the proxy server. Requires <see cref="ProxyPassword"/>.
193+
/// </summary>
194+
/// <remarks>Only applies when <see cref="UseWebSockets"/> = <see langword="true"/></remarks>
195+
public Layout ProxyLogin { get; set; }
196+
197+
/// <summary>
198+
/// Password to use for the proxy server. Requires <see cref="ProxyLogin"/>.
199+
/// </summary>
200+
/// <remarks>Only applies when <see cref="UseWebSockets"/> = <see langword="true"/></remarks>
201+
public Layout ProxyPassword { get; set; }
202+
203+
/// <summary>
204+
/// Uses the default credentials (<see cref="System.Net.CredentialCache.DefaultCredentials"/>) for the proxy server, overriding any values that may have been set in <see cref="ProxyLogin"/> and <see cref="ProxyPassword"/>.
205+
/// </summary>
206+
/// <remarks>Only applies when <see cref="UseWebSockets"/> = <see langword="true"/></remarks>
207+
public bool UseDefaultCredentialsForProxy { get; set; }
208+
171209
/// <summary>
172210
/// Gets a list of user properties (aka custom properties) to add to the AMQP message
173211
/// </summary>
@@ -225,6 +263,7 @@ protected override void InitializeTarget()
225263
string useWebSockets = string.Empty;
226264
string webSocketProxyAddress = string.Empty;
227265
string customEndPointAddress = string.Empty;
266+
string eventProducerIdentifier = string.Empty;
228267

229268
var defaultLogEvent = LogEventInfo.CreateNullEvent();
230269

@@ -245,15 +284,26 @@ protected override void InitializeTarget()
245284
clientAuthSecret = ClientAuthSecret?.Render(defaultLogEvent);
246285
}
247286

287+
eventProducerIdentifier = EventProducerIdentifier?.Render(defaultLogEvent) ?? string.Empty;
288+
248289
useWebSockets = UseWebSockets?.Render(defaultLogEvent) ?? string.Empty;
249290
if (!string.IsNullOrEmpty(useWebSockets) && (string.Equals(useWebSockets.Trim(), bool.TrueString, StringComparison.OrdinalIgnoreCase) || string.Equals(useWebSockets.Trim(), "1", StringComparison.OrdinalIgnoreCase)))
250291
{
251292
useWebSockets = bool.TrueString;
252293
}
253294
customEndPointAddress = CustomEndpointAddress?.Render(defaultLogEvent) ?? string.Empty;
254-
webSocketProxyAddress = WebSocketProxyAddress?.Render(defaultLogEvent) ?? string.Empty;
255295

256-
_eventHubService.Connect(connectionString, eventHubName, serviceUri, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, storageAccountName, storageAccountAccessKey, clientAuthId, clientAuthSecret, bool.TrueString == useWebSockets, webSocketProxyAddress, customEndPointAddress);
296+
var proxySettings = new ProxySettings
297+
{
298+
NoProxy = NoProxy,
299+
UseDefaultCredentials = UseDefaultCredentialsForProxy,
300+
Address = ProxyAddress?.Render(defaultLogEvent),
301+
Login = ProxyLogin?.Render(defaultLogEvent),
302+
Password = ProxyPassword?.Render(defaultLogEvent)
303+
};
304+
proxySettings = proxySettings.RequiresManualProxyConfiguration ? proxySettings : null;
305+
306+
_eventHubService.Connect(connectionString, eventHubName, serviceUri, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, storageAccountName, storageAccountAccessKey, clientAuthId, clientAuthSecret, eventProducerIdentifier, bool.TrueString == useWebSockets, customEndPointAddress, proxySettings);
257307
InternalLogger.Debug("AzureEventHubTarget(Name={0}): Initialized", Name);
258308
}
259309
catch (Exception ex)
@@ -556,17 +606,21 @@ private sealed class EventHubService : IEventHubService
556606

557607
public string EventHubName { get; private set; }
558608

559-
public void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, bool useWebSockets, string webSocketsProxyAddress, string endPointAddress)
609+
public void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, string eventProducerIdentifier, bool useWebSockets, string endPointAddress, ProxySettings proxySettings)
560610
{
561611
EventHubName = eventHubName;
562612

563613
Azure.Messaging.EventHubs.Producer.EventHubProducerClientOptions options = default;
564-
if (useWebSockets || !string.IsNullOrEmpty(webSocketsProxyAddress) || !string.IsNullOrEmpty(endPointAddress))
614+
if (useWebSockets || !string.IsNullOrEmpty(endPointAddress) || !string.IsNullOrEmpty(eventProducerIdentifier))
565615
{
566616
options = new Azure.Messaging.EventHubs.Producer.EventHubProducerClientOptions();
567617
options.ConnectionOptions.TransportType = useWebSockets ? EventHubsTransportType.AmqpWebSockets : options.ConnectionOptions.TransportType;
568-
options.ConnectionOptions.Proxy = !string.IsNullOrEmpty(webSocketsProxyAddress) ? new System.Net.WebProxy(webSocketsProxyAddress, true) : options.ConnectionOptions.Proxy;
569-
options.ConnectionOptions.CustomEndpointAddress = !string.IsNullOrEmpty(endPointAddress) ? new Uri(endPointAddress) : options.ConnectionOptions.CustomEndpointAddress;
618+
if (useWebSockets && proxySettings != null)
619+
options.ConnectionOptions.Proxy = proxySettings.CreateWebProxy(options.ConnectionOptions.Proxy);
620+
if (!string.IsNullOrEmpty(endPointAddress))
621+
options.ConnectionOptions.CustomEndpointAddress = new Uri(endPointAddress);
622+
if (!string.IsNullOrEmpty(eventProducerIdentifier))
623+
options.Identifier = eventProducerIdentifier;
570624
}
571625

572626
if (string.IsNullOrWhiteSpace(serviceUri))

src/NLog.Extensions.AzureEventHub/IEventHubService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Azure.Messaging.EventHubs;
5+
using NLog.Extensions.AzureBlobStorage;
56

67
namespace NLog.Extensions.AzureStorage
78
{
89
internal interface IEventHubService
910
{
1011
string EventHubName { get; }
11-
void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, bool useWebSockets, string webSocketsProxyAddress, string endPointAddress);
12+
void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, string eventProducerIdentifier, bool useWebSockets, string endPointAddress, ProxySettings proxySettings);
1213
Task CloseAsync();
1314
Task SendAsync(IEnumerable<EventData> eventDataBatch, string partitionKey, CancellationToken cancellationToken);
1415
}

0 commit comments

Comments
 (0)