NLog DataTablesTarget for writing to Azure Tables in Azure storage accounts or Azure Cosmos DB table API
$ dotnet add package NLog.Extensions.AzureDataTables| Package Name | NuGet | Description |
|---|---|---|
| NLog.Extensions.AzureDataTables | Azure Table Storage or Azure CosmosDb Tables |
Supports both Azure Storage Tables and CosmosDB Tables.
<extensions>
<add assembly="NLog.Extensions.AzureDataTables" />
</extensions>
<targets>
<target xsi:type="AzureDataTables"
name="String"
layout="Layout"
connectionString="String"
tableName="Layout"
logTimeStampFormat="O" />
</targets>
name - Name of the target.
layout - Text to be rendered. Layout Required.
connectionString - Azure storage connection string. Layout. Required unless using serviceUri.
serviceUri - Alternative to ConnectionString, where Managed Identiy is acquired from DefaultAzureCredential.
tableName - Azure table name. Layout
rowKey - Azure Table RowKey. Layout. Default = "InverseTicks_${guid}"
partitionKey - Azure PartitionKey. Layout. Default = ${logger}
logTimeStampFormat - Default Log TimeStamp is set to 'O' for Round-trip format if not specified.
managedIdentityClientId - clientId for 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 TableSharedKeyCredential authentication. Requires serviceUri and accessKey.
accessKey - accountKey for TableSharedKeyCredential authentication. Requires serviceUri and accountName.
clientAuthId - clientId for ClientSecretCredential OAuth2 authentication. Requires serviceUri, tenantIdentity and clientAuthSecret.
clientAuthSecret - clientSecret for ClientSecretCredential OAuth2 authentication. Requires serviceUri,tenantIdentity and clientAuthId.
When using ServiceUri (Instead of ConnectionString) with DefaultAzureCredential, then Azure Identity can also resolve from environment variables:
AZURE_CLIENT_ID - For ManagedIdentityClientId / WorkloadIdentityClientIdAZURE_TENANT_ID - For TenantIdSee also: Set up Your Environment for Authentication
noProxy - Bypasses any system proxy and proxy in ProxyAddress when set to true.
proxyAddress - Address of the proxy server to use (e.g. http://proxyserver:8080).
proxyLogin - Login to use for the proxy server. Requires proxyPassword.
proxyPassword - Password to use for the proxy server. Requires proxyLogin.
useDefaultCredentialsForProxy - Uses the default credentials (System.Net.CredentialCache.DefaultCredentials) for the proxy server. Take precedence over proxyLogin and proxyPassword when set to true.
Instead of using the predefined NLogEntity-properties, then one can specify wanted properties:
<extensions>
<add assembly="NLog.Extensions.AzureDataTables" />
</extensions>
<targets>
<target xsi:type="AzureDataTables"
name="String"
connectionString="Layout"
tableName="Layout">
<contextproperty name="Level" layout="${level}" />
<contextproperty name="LoggerName" layout="${logger}" />
<contextproperty name="Message" layout="${message:raw=true}" />
<contextproperty name="Exception" layout="${exception:format=tostring}" />
<contextproperty name="FullMessage" layout="${message}" />
<contextproperty name="MachineName" layout="${machinename}" />
</target>
</targets>
It will by default include the hardcoded property LogTimeStamp of type DateTime that contains LogEventInfo.TimeStamp.ToUniversalTime().
<contextproperty name="LogTimeStamp"> as the first property, where empty property-value means leave out.batchSize - Number of EventData items 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)
There are restrictions for how big column values can be:
When breaking these limits, then Azure Table Service will discard the data, so NLog AzureDataTables will automatically truncate if needed.
NLog Layout makes it possible to retrieve settings from many locations.
connectionString="${configsetting:ConnectionStrings.AzureTable}"
{
"ConnectionStrings": {
"AzureTable": "Server=tcp:server.database.windows.net;"
}
}
connectionString="${appsetting:ConnectionStrings.AzureTable}"
<configuration>
<connectionStrings>
<add name="AzureTable" connectionString="Server=tcp:server.database.windows.net;"/>
</connectionStrings>
</configuration>
connectionString="${environment:AZURESQLCONNSTR_CONNECTION_STRING}"
connectionString="${gdc:AzureTableConnectionString}"
NLog.GlobalDiagnosticsContext.Set("AzureTableConnectionString", "Server=tcp:server.database.windows.net;");