从 'System.String' 到 'Serilog.Core.IDestructuringPolicy' 的转换无效
Invalid cast from 'System.String' to 'Serilog.Core.IDestructuringPolicy'
通过学习Serilog.Sinks.AzureTableStorage我有以下
主要
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration) // the error happens here
.CreateLogger();
logger.Information("Hello, world!");
在appsetttings.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"
}
}
我在调试输出中看到,但没有数据记录到存储 table。
Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll
An unhandled exception of type 'System.InvalidCastException' occurred in System.Private.CoreLib.dll
Invalid cast from 'System.String' to 'Serilog.Core.IDestructuringPolicy'.
[更新]
如果我按照 GitHub 中的 ReadMe.Md 使用非常简单的配置,我会得到同样的错误。
[更新]
我从 Kennyzx link 复制了代码。错误已更改为
System.InvalidCastException
HResult=0x80004002
Message=Invalid cast from 'System.String' to 'Serilog.Core.ILogEventFilter'.
Source=System.Private.CoreLib
我决定尝试将 serilog-settings-configuration 示例项目升级到 .netcore2.1,因此询问 this question
几个小时后我得出的结论是 serilog-settings-configuration s 与 dotnetcore 2.1 不兼容
我尝试设置一个名为 UseSerilog 的新 .NET Core 2.1 控制台项目,我可以重现该问题。
在我从配置中删除 Destructure
和 Filter
部分后,应用程序开始运行。
然后我检查了Serilog.Settings.Configuration
包的源代码,我找到了this commit,我得出结论,你需要写一些代码才能让它像这样工作。
对于下面的配置,你需要在实现IDestructuringPolicy
.
的"Sample"命名空间中写一个CustomPolicy
class
{
"Name": "With",
"Args": { "policy": "Sample.CustomPolicy, Sample" }
}
如果您删除它,并保留 Destructure
如下所示,它可以正常工作。
"Destructure": [
{
"Name": "ToMaximumDepth",
"Args": { "maximumDestructuringDepth": 3 }
},
{
"Name": "ToMaximumStringLength",
"Args": { "maximumStringLength": 10 }
},
{
"Name": "ToMaximumCollectionCount",
"Args": { "maximumCollectionCount": 5 }
}
]
希望这一发现对您有所帮助。
通过学习Serilog.Sinks.AzureTableStorage我有以下
主要
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration) // the error happens here
.CreateLogger();
logger.Information("Hello, world!");
在appsetttings.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"
}
}
我在调试输出中看到,但没有数据记录到存储 table。
Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll
An unhandled exception of type 'System.InvalidCastException' occurred in System.Private.CoreLib.dll
Invalid cast from 'System.String' to 'Serilog.Core.IDestructuringPolicy'.
[更新]
如果我按照 GitHub 中的 ReadMe.Md 使用非常简单的配置,我会得到同样的错误。
[更新]
我从 Kennyzx link 复制了代码。错误已更改为
System.InvalidCastException
HResult=0x80004002
Message=Invalid cast from 'System.String' to 'Serilog.Core.ILogEventFilter'.
Source=System.Private.CoreLib
我决定尝试将 serilog-settings-configuration 示例项目升级到 .netcore2.1,因此询问 this question
几个小时后我得出的结论是 serilog-settings-configuration s 与 dotnetcore 2.1 不兼容
我尝试设置一个名为 UseSerilog 的新 .NET Core 2.1 控制台项目,我可以重现该问题。
在我从配置中删除 Destructure
和 Filter
部分后,应用程序开始运行。
然后我检查了Serilog.Settings.Configuration
包的源代码,我找到了this commit,我得出结论,你需要写一些代码才能让它像这样工作。
对于下面的配置,你需要在实现IDestructuringPolicy
.
CustomPolicy
class
{
"Name": "With",
"Args": { "policy": "Sample.CustomPolicy, Sample" }
}
如果您删除它,并保留 Destructure
如下所示,它可以正常工作。
"Destructure": [
{
"Name": "ToMaximumDepth",
"Args": { "maximumDestructuringDepth": 3 }
},
{
"Name": "ToMaximumStringLength",
"Args": { "maximumStringLength": 10 }
},
{
"Name": "ToMaximumCollectionCount",
"Args": { "maximumCollectionCount": 5 }
}
]
希望这一发现对您有所帮助。