NLog ServiceBusTarget for writing to Azure Service Bus Topic or Queue
$ dotnet add package NLog.Extensions.AzureServiceBus| Package Name | NuGet | Description |
|---|---|---|
| NLog.Extensions.AzureServiceBus | Azure Service Bus |
<extensions>
<add assembly="NLog.Extensions.AzureServiceBus" />
</extensions>
<targets>
<target xsi:type="AzureServiceBus"
name="String"
layout="Layout"
connectionString="Layout"
queueName="Layout"
topicName="Layout"
sessionId="Layout"
partitionKey="Layout"
subject="Layout"
contentType="Layout"
messageId="Layout"
correlationId="Layout">
<messageProperty name="level" layout="${level}" />
<messageProperty name="exception" layout="${exception:format=shorttype}" includeEmptyValue="false" />
<layout type="JsonLayout" includeAllProperties="true">
<attribute name="time" layout="${longdate}" />
<attribute name="message" layout="${message}" />
<attribute name="threadid" layout="${threadid}" />
<attribute name="exception" layout="${exception:format=tostring}" />
</layout>
</target>
</targets>
name - Name of the target.
connectionString - Azure storage connection string. Layout Required unless using ServiceUri.
serviceUri - Alternative to ConnectionString, where Managed Identiy is applied from DefaultAzureCredential.
queueName - QueueName for multiple producers single consumer. Layout
- TopicName for multiple producers and multiple subscribers.
sessionId - SessionId-Key which Service Bus uses to generate PartitionId-hash. Layout
partitionKey - Partition-Key which Service Bus uses to generate PartitionId-hash. Layout
subject - Service Bus Message Subject to be used as label for the message. Layout
layout - Service Bus Message Body to be rendered and encoded as UTF8. Layout Required.
contentType - Service Bus Message Body ContentType. Layout. Ex. application/json
messageId - Service Bus Message MessageId. Layout
correlationId - Service Bus Message Correlationid. Layout
timeToLiveSeconds - Default Time-To-Live (TTL) for ServiceBus messages in seconds (Optional)
timeToLiveDays - Default Time-To-Live (TTL) for ServiceBus messages in days (Optional)
useWebSockets - Enable AmqpWebSockets. Ex. true/false (optional)
customEndpointAddress - Custom endpoint address that can be used when establishing the connection (optional)
eventProducerIdentifier - A unique name used to identify the event producer. If null or empty, a GUID will be used as the identifier (optional)
managedIdentityClientId - Sets ManagedIdentityClientId on DefaultAzureCredentialOptions. Requires serviceUri.
managedIdentityResourceId - resourceId for ManagedIdentityResourceId on DefaultAzureCredentialOptions, do not use together with ManagedIdentityClientId. Requires serviceUri.
tenantIdentity - tenantId for DefaultAzureCredentialOptions. Requires serviceUri.
sharedAccessSignature - Access signature for AzureSasCredential authentication. Requires serviceUri.
accountName - accountName for AzureNamedKeyCredential authentication. Requires serviceUri and accessKey.
accessKey - accountKey for AzureNamedKeyCredential authentication. Requires serviceUri and accountName.
clientAuthId - clientId for ClientSecretCredential authentication. Requires serviceUri, tenantIdentity and clientAuthSecret.
clientAuthSecret - clientSecret for ClientSecretCredential authentication. Requires serviceUri,tenantIdentity and clientAuthId.
noProxy - Bypasses any system proxy and proxy in ProxyAddress when set to true. Requires useWebSockets = true.
proxyAddress - Address of the proxy server to use (e.g. http://proxyserver:8080). Requires useWebSockets = true.
proxyLogin - Login to use for the proxy server. Requires proxyPassword. Requires useWebSockets = true.
proxyPassword - Password to use for the proxy server. Requires proxyLogin. Requires useWebSockets = true.
useDefaultCredentialsForProxy - Uses the default credentials (System.Net.CredentialCache.DefaultCredentials) for the proxy server. Take precedence over proxyLogin and proxyPassword when set to true. Requires useWebSockets = true.
maxBatchSizeBytes - Max size of a single batch in bytes Integer (Default=256*1024)
batchSize - Number of LogEvents to send in a single batch (Default=100)
taskDelayMilliseconds - Artificial delay before sending to optimize for batching (Default=200 ms)
queueLimit - Number of pending LogEvents to have in memory queue, that are waiting to be sent (Default=10000)
overflowAction - Action to take when reaching limit of in memory queue (Default=Discard)
taskTimeoutSeconds - How many seconds a Task is allowed to run before it is cancelled (Default 150 secs)
retryDelayMilliseconds - How many milliseconds to wait before next retry (Default 500ms, and will be doubled on each retry).
retryCount - How many attempts to retry the same Task, before it is aborted (Default 0)
When using ServiceUri (Instead of ConnectionString), then DefaultAzureCredential is used for Azure Identity which supports environment variables:
AZURE_CLIENT_ID - For ManagedIdentityClientId / WorkloadIdentityClientIdAZURE_TENANT_ID - For TenantIdNLog Layout makes it possible to retrieve settings from many locations.
connectionString="${configsetting:ConnectionStrings.AzureBus}"
{
"ConnectionStrings": {
"AzureBus": "UseDevelopmentStorage=true;"
}
}
connectionString="${appsetting:ConnectionStrings.AzureBus}"
<configuration>
<connectionStrings>
<add name="AzureBus" connectionString="UseDevelopmentStorage=true;"/>
</connectionStrings>
</configuration>
connectionString="${environment:AZURE_STORAGE_CONNECTION_STRING}"
connectionString="${gdc:AzureBusConnectionString}"
NLog.GlobalDiagnosticsContext.Set("AzureBusConnectionString", "UseDevelopmentStorage=true;");