如何将我的日志从 Asp Net Core WebApi 存储到 Azure Table Store?

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

我有 ASP 使用 .Net Framework 4.6 的 .Net Core WebApi 应用程序。我正在使用 NLog 进行日志记录。我想登录到 Azure Table 存储。为此,我正在使用 AzureTableStorageNLogTarget 和 NLogExtensions.Logging Nuget 包。

Azure 的最新稳定版本TableStorageNLogTarget 是 1.0.9 但是当我使用它时出现以下异常:

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

所以我使用的是 1.0.8 版本。

现在我遇到以下异常:

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

谷歌搜索错误让我无法找到 Azure Table 存储连接字符串,我不明白为什么。

这是我的 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>

还有我的 Appsetting.json:

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

}

示例项目可在 Git 上获得:https://github.com/pankyog/NLogToAzureTable/tree/Re1

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

据我所知,AzureTableStorageNLogTarget(版本 1.0.9)中名为 NLog.Extensions.AzureTableStorage.dll 的引用引用了 Microsoft.WindowsAzure.ConfigurationManager 版本 3.0.0.0。而 WindowsAzure.Storage (4.3.0) 引用 Microsoft.WindowsAzure.ConfigurationManager 版本 1.8.0.

注意:Microsoft.WindowsAzure.ConfigurationManager3.0.0.0中,CloudConfigurationManager的命名空间已经从Microsoft.WindowsAzure移动到Microsoft.Azure。

您可以参考 WindowsAzure.Storage 的最新稳定版本并参考 3.0.0.0 的 Microsoft.WindowsAzure.ConfigurationManager 来解决此错误。

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

NLog Azure Table Storage Target所述:

[[connection-string-key]]是应用设置或云服务配置文件中Azure Storage Account连接字符串设置的key。

在 ASP.NET Core 中,设置已移至 appsettings.json。您可以手动添加 App.config 文件并为开发配置 Azure 存储帐户连接字符串设置。此外,您可以通过 Azure 门户在 Web 应用程序中配置应用程序设置,以在将 Web 应用程序部署到 Azure 时覆盖存储帐户连接字符串。