使用appsettings.json时,Azure功能无法初始化Serilog Sink

问题描述 投票:5回答:1

我创建了一个简单的Azure功能,旨在使用Serilog记录到Azure Blob存储。

当使用Serilog接收器的内联配置时,它可以完美地工作,接收器被创建,Serilog很乐意接收Blob存储。

这有效:

Log.Logger = new LoggerConfiguration()
   .WriteTo.AzureBlobStorage(
           "STORAGEACCOUNT CONNECTION STRING", // <-- inline config
           LogEventLevel.Information,
           null,
           "{yyyy}/{MM}/{dd}/MyLogFile.txt")
   .CreateLogger();

enter image description here

问题是我想通过appsettings.json配置这一切,但是当我尝试这个(在模拟器和云中本地运行)时,它无法绑定接收器设置,因此无法登录到博客存储。

如果我调试,我可以看到配置值按预期加载到configuration对象中,但它们没有应用于日志记录配置。

这不起作用:

Log.Logger = new LoggerConfiguration()
   .ReadFrom.Configuration(configuration)
   .CreateLogger();

appsettings.json在这个片段中:

{

  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information"
      }
    },
    "WriteTo": [{
        "Name": "AzureBlobStorage",
        "Args": {
          "connectionString": " --- REPLACE WITH STORAGEACCOUNT BLOB CONNECTION STRING --- ",
          "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact",
          "storageFileName": "{yyyy}/{MM}/{dd}/MyLogFile.txt",
          "retainedFileCountLimit": 31
        }
      }

    ],
    "Properties": {
      "Application": "int-test-logging",
      "Environment": "int"
    }
  }

}

enter image description here

我不确定我做错了什么,但任何帮助将不胜感激。以下github repo包含实现上述(两种方法)的代码,并重现此行为。 https://github.com/oneiltomlinson/AzureFunctionsLogging

azure azure-functions serilog
1个回答
6
投票

只需添加

    "Using": [ "Serilog.Sinks.AzureBlobStorage" ] //under Serilog Config

它会工作!

工作配置

{
    "Serilog": {
        "Using": [
            "Serilog.Sinks.AzureBlobStorage"
        ],
        "MinimumLevel": {
            "Default": "Information",
            "Override": {
                "Microsoft": "Information"
            }
        },
        "WriteTo": [
            {
                "Name": "AzureBlobStorage",
                "Args": {
                    "connectionString": " --- REPLACE WITH STORAGEACCOUNT BLOB CONNECTION STRING --- ",
                    "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact",
                    "storageFileName": "{yyyy}/{MM}/{dd}/MyLogFile.txt",
                    "retainedFileCountLimit": 31
                }
            }
        ],
        "Properties": {
            "Application": "int-test-logging",
            "Environment": "int"
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.