通过研究 Serilog.Sinks.AzureTableStorage,我得出以下结论:
在 Main 函数中:
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration) // the error happens here
.CreateLogger();
logger.Information("Hello, world!");
在appsettings.json中(使用不同的连接字符串)
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.AzureTableStorage" ],
"MinimumLevel": "Debug",
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" }
},
{
"Name": "AzureTableStorage",
"Args": {
"storageTableName": "mystoragetable",
"connectionString": "myconnectionstring"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Destructure": [
{
"Name": "With",
"Args": { "policy": "Sample.CustomPolicy, Sample" }
},
{
"Name": "ToMaximumDepth",
"Args": { "maximumDestructuringDepth": 4 }
},
{
"Name": "ToMaximumStringLength",
"Args": { "maximumStringLength": 100 }
},
{
"Name": "ToMaximumCollectionCount",
"Args": { "maximumCollectionCount": 10 }
}
],
"Properties": {
"Application": "Sample"
}
}
我在调试输出中看到没有数据被记录到存储表中。
'System.Private.CoreLib.dll'中引发了异常:'System.InvalidCastException'
类型为'System.InvalidCastException'的未处理异常已在'System.Private.CoreLib.dll'中发生
从'System.String'到'Serilog.Core.IDestructuringPolicy'的转换无效。
[更新]
如果我按照GitHub上的ReadMe.Md使用非常简单的配置,我会得到相同的错误。
[更新]
我复制了Kennyzx链接中的代码。错误已更改为
System.InvalidCastException
HResult=0x80004002
Message=Invalid cast from 'System.String' to 'Serilog.Core.ILogEventFilter'.
Source=System.Private.CoreLib
我决定尝试将serilog-settings-configuration示例项目升级到.netcore2.1,因此我提出了这个问题
几个小时后,我得出结论,serilog-settings-configuration不兼容dotnetcore 2.1。