我创建了一个简单的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();
问题是我想通过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"
}
}
}
我不确定我做错了什么,但任何帮助将不胜感激。以下github repo包含实现上述(两种方法)的代码,并重现此行为。 https://github.com/oneiltomlinson/AzureFunctionsLogging
只需添加
"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"
}
}
}