NLog.Extensions.AzureTableStorage 1.2.0-rc1

This is a prerelease version of NLog.Extensions.AzureTableStorage.
dotnet add package NLog.Extensions.AzureTableStorage --version 1.2.0-rc1
NuGet\Install-Package NLog.Extensions.AzureTableStorage -Version 1.2.0-rc1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NLog.Extensions.AzureTableStorage" Version="1.2.0-rc1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NLog.Extensions.AzureTableStorage --version 1.2.0-rc1
#r "nuget: NLog.Extensions.AzureTableStorage, 1.2.0-rc1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NLog.Extensions.AzureTableStorage as a Cake Addin
#addin nuget:?package=NLog.Extensions.AzureTableStorage&version=1.2.0-rc1&prerelease

// Install NLog.Extensions.AzureTableStorage as a Cake Tool
#tool nuget:?package=NLog.Extensions.AzureTableStorage&version=1.2.0-rc1&prerelease

NLog target for Azure Storage Tables

This package allows you to set up an NLog target to store logs in Azure table storage.

Changes for version 1.2

Currently in 'RC' status because it requires NLog 4.5-rc. Added support for .NET Core.

Changes for version 1.1.4

Merged pull request from arangas which added the ${level} macro.

Changes for version 1.1.3.2

NOTE: Upgrading from 1.1.0 to 1.1.3.2 will break your configuration files!

Previous versions required the developer to store the azure storage connection string in app.config, web.config or cloud config, and then assign the key to the 'ConnectionStringKey' property of the NLog target.

The new release replaces the 'ConnectionStringKey' property with the 'ConnectionString' property. This allows us to either specify the connection string outright in NLog.config (thus not needing another config file), or to intitialize it on the fly, for example in global.asax, from an environment-dependent config setting.

How to use

  • Download package NLog.Extensions.AzureTableStorage from the nuget site</a>.
  • Set up your NLog.config file:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  
  <extensions>
    <add assembly="NLog.Extensions.AzureTableStorage"/>
  </extensions>
  
  
  <targets>
    <target name="AzureTableStorage" 
        xsi:type="AzureTableStorage" 
        PartitionKey="${date}.${logger}" 
        RowKey="${ticks}.${guid}"
        ConnectionString="UseDevelopmentStorage=true" 
        tableName="TempAzureTableStorageTargetTestsLogs" />
  </targets>
  
  <rules>
    
    <logger name="*" minlevel="Trace" writeTo="AzureTableStorage" />
  </rules>
</nlog>

A real Azure storage account connection string will look something like this:

...
  <target ...
  ConnectionString="DefaultEndpointsProtocol=https;AccountName=igdevpdf;AccountKey=xxxxxxx==" 
  ... />
...  

In this config file, the following parameters can be used to configure your target:

  • name is the name of your target.
  • xsi:type must be 'AzureTableStorage'.
  • PartitionKey is a string which contains macros and string literals, more on that below.
  • RowKey uses the same syntax as PartitionKey.
  • ConnectionString is the Azure storage connection string. Make it an empty string if you are planning to initialize it at runtime.
  • tableName is the name of the azure storage table. If the table does not exist, it will automatically be created.

Obviously, PartitionKey and RowKey cannot just be left as constants, we need to be able to vary them. This package allows you to use the following macros to format your partition and row keys:

  • ${date} will be replaced with an 8 symbol date (e.g. 20150926).
  • ${time} will be replaced with a 6 digit time (e.g. 221110).
  • ${ticks} will be replaced with a 19 digit number of <a href="https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx">ticks</a>.
  • ${longdate} will be replaced with a 20 symbol long date which includes year, month, day, hour, minutes, seconds and millionths of a second.
  • ${micros} will be replaced with a 6 digit number of millionths of a second (microseconds).
  • ${guid} will be replaced with a new random GUID.
  • ${logger} will be replaced by the name of the current class logger.
  • ${level} will be replaced by the log level. Also supports ${level:uppercase=true}.
  • ${machine} will be replaced by the value of Environment.Machine.
  • ${descticks} will be replaced by the number of ticks remaining till <a href="https://msdn.microsoft.com/en-us/library/system.datetime.maxvalue(v=vs.110).aspx">DateTime.MaxValue</a>.

Configuring the Target Dynamically

You can initialize the target's connection string programmatically, for example, from Global.asax.cs:

protected void Application_Start()
{
  var azureStorageTarget = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName("AzureStorage");
  azureStorageTarget.ConnectionString = "actual conn. string // or pull from web.config";
  LogManager.ReconfigExistingLoggers();
}

If you do that, you may want to set the 'ConnectionString' property of the AzureTableStorage target in NLog.config to an empty string. That will prevent you from accidentally logging something to an un-intended location before you initialized it.

How to View Logs?

I have found that the "Cloud Explorer" built into Microsoft Visual Studio 2015+ is sufficient for most of my log searching needs (as of this writing, VS 2017 is still the best way to view azure tables). Think hard about what you are going to use as PartitionKey and RowKey.

Other ways to access table storage (not sure about how current these are):

  • <a href="http://www.cloudportam.com/">Cloud Portam</a>
  • <a href="http://azurestorageexplorer.codeplex.com/">Azure Storage Explorer</a>

What if I Run Into Errors

If you deploy your application and you are seeing this error:

No connection could be made because the target machine actively refused it 127.0.0.1:10002 

it means that after resolving your configuration, the AzureTableStorage target's ConnectionString property is still set to "UseDevelopmentStorage=true" and the process is trying to connect to a (non-existent) Windows Azure emulator on your production web server.

Running Unit Tests

Before running tests on you local machine, make sure Azure Storage Emulator is running. I think that I need to change the tests to have a setup/teardown executed before each individual unit test. I believe that multiple records run into each other when the tests are all run as a batch.

About NLog Targets

For more info about NLog targets and how to use it, refer to <a href="https://github.com/nlog/NLog/wiki/How%20to%20write%20a%20Target">How To Write a Target</a>.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NLog.Extensions.AzureTableStorage:

Package Downloads
K0NRT15.App.Log

K0NRT15 Core App Logging Toolkit

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0-rc1 65,966 3/1/2018
1.2.0-rc0 4,307 3/1/2018
1.1.4 291,719 1/5/2016
1.1.3.2 11,656 10/6/2015
1.1.0 3,962 10/1/2015