Pankaj
Pankaj

Reputation: 27

How do I store my Logs to Azure Table Store from Asp Net Core WebApi?

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

Upvotes: 1

Views: 2751

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

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

Upvotes: 1

Related Questions