从'System.String'到'Serilog.Core.IDestructuringPolicy'的转换无效。

17

通过研究 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。


https://github.com/serilog/serilog-sinks-console/issues/28 - Kirsten
“Serilog.Core.IDestructuringPolicy” 接口的定义是什么?你能发布一下代码吗? - kennyzx
我从Github下载了代码,但界面不显示。该代码使用的是.NetCoreApp 1.0版本,而我使用的是2.1.403版本。 - Kirsten
1
我猜问题一定是因为我使用了不同版本的 .netcore。 - Kirsten
1
请尝试此示例 - kennyzx
显示剩余2条评论
1个回答

39

我尝试创建一个名为 UseSerilog 的新 .NET Core 2.1 控制台项目,并且我能够重现这个问题。

当我从配置中删除DestructureFilter部分后,应用程序开始工作。

然后我检查了Serilog.Settings.Configuration包的源代码,发现了这个提交记录,并得出结论,你需要编写一些代码才能使它以这种方式工作。

对于以下配置,您需要在“Sample”命名空间中编写一个实现IDestructuringPolicyCustomPolicy类。

{
    "Name": "With",
    "Args": { "policy": "Sample.CustomPolicy, Sample" }
}
如果您删除这部分内容,保留下面的 Destructure,它就可以工作。
"Destructure": [
  {
    "Name": "ToMaximumDepth",
    "Args": { "maximumDestructuringDepth": 3 }
  },
  {
    "Name": "ToMaximumStringLength",
    "Args": { "maximumStringLength": 10 }
  },
  {
    "Name": "ToMaximumCollectionCount",
    "Args": { "maximumCollectionCount": 5 }
  }
]
希望这项发现对你有所帮助。

1
谢谢。使用CustomPolicy也可以,只是我意识到我需要编写它。 - Kirsten

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接