1

I have ASP .Net Core WebApi application that is using .Net Framework 4.6. I am using NLog for logging. I want to log to Azure Table Storage. For that I am using AzureTableStorageNLogTarget and NLogExtensions.Logging Nuget Packages.

Latest Stable Release for AzureTableStorageNLogTarget is 1.0.9 but when I use that I am getting following Exception:

Could not load type 'Microsoft.Azure.CloudConfigurationManager' from assembly 

So I am using version 1.0.8.

Now I am getting following exception:

Error Error initializing target 'AzureTableStorage Target[AzureTableStorageTarget]'. Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: connectionString

Googling the error gives me that It is not able to find Azure Table Storage ConnectionString, I don't understand why.

Here is my NLog.Config

<?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"
      autoReload="true"
      internalLogLevel="Warn"
      internalLogFile="c:\temp\internal-nlog.txt">
  <extensions>
    <add assembly="NLog.Extensions.AzureTableStorage"/>
  </extensions>
  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="c:\temp\WebAPI-nlog-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />


    <target xsi:type="File" name="ownFile-web" fileName="c:\temp\WebAPI-nlog-own-${shortdate}.log"
             layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />


    <target xsi:type="AzureTableStorage"
            name="AzureTableStorageTarget"
            ConnectionStringKey="AzureStorageConnectionString"
            TableName="LogTable" />
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
    <logger name="*" minlevel="Trace" writeTo="AzureTableStorageTarget" />
  </rules>
</nlog>

And my Appsetting.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "AppSettings": {
    "AzureStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=sqdev01storage;AccountKey=<key redacted>"
  }

}

A sample project is available on Git at: https://github.com/pankyog/NLogToAzureTable/tree/Re1

juunas
  • 54,244
  • 13
  • 113
  • 149
Pankaj
  • 27
  • 7

1 Answers1

1

Could not load type 'Microsoft.Azure.CloudConfigurationManager' from assembly

As I know, The reference named NLog.Extensions.AzureTableStorage.dll in AzureTableStorageNLogTarget (version 1.0.9), which references the Microsoft.WindowsAzure.ConfigurationManager version 3.0.0.0. While, the WindowsAzure.Storage (4.3.0) references the Microsoft.WindowsAzure.ConfigurationManager version 1.8.0.

Note: In Microsoft.WindowsAzure.ConfigurationManager 3.0.0.0, the namespace of CloudConfigurationManager has moved from Microsoft.WindowsAzure to Microsoft.Azure.

You could reference the latest stable release for WindowsAzure.Storage and reference the Microsoft.WindowsAzure.ConfigurationManager for 3.0.0.0 to resolve this error.

Error Error initializing target 'AzureTableStorage Target[AzureTableStorageTarget]'. Exception: System.ArgumentNullException: Value cannot be null. Parameter name: connectionString

As mentioned in NLog Azure Table Storage Target:

[[connection-string-key]] is the key of Azure Storage Account connection string setting in App Settings or Cloud Service configuration file.

In ASP.NET Core, the settings have been moved to appsettings.json. You could manually add a App.config file and configure your Azure Storage Account connection string setting for development. Also, you could configure appsettings in your web app via Azure Portal to override the storage account connection string when your web app is deployed to Azure.

1

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • I already have defined connection string in AppSettings section in appsettings.json file. Why do I need a different App.Config file. Also if I create a new app.config file do I need to update code to read it? Because I did not see any code in Startup.cs to read that. – Pankaj Nov 04 '16 at 21:02
  • I followed [NLog Azure Table Storage Target](https://github.com/harouny/NLog.Extensions.AzureTableStorage) to configure my Azure Storage Account connection string. And I could reproduce your issue when setting connection string in appsettings.json file. After I added a addtional app.config file, I could work it as expected on my side without updating any code to read the settings. I assumed that `NLog.Config` couldn't recognise the settings from appsettings.json. Please try to modify your settings as I mentioned and find whether you could resolve this issue. – Bruce Chen Nov 05 '16 at 03:32