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

Kir*_*eed 7 c# serilog .net-core

通过研究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!");
Run Code Online (Sandbox Code Playgroud)

在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"
    }    
  }
Run Code Online (Sandbox Code Playgroud)

我在调试输出中看到,但没有数据记录到存储表中。

抛出异常:System.Private.CoreLib.dll中的“ System.InvalidCastException”

System.Private.CoreLib.dll中发生了类型为'System.InvalidCastException'的未处理异常

从“ 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
Run Code Online (Sandbox Code Playgroud)

我决定尝试将serilog-settings-configuration示例项目升级到.netcore2.1,因此提出了这个问题

几个小时后,我得出的结论是serilog-settings-configuration与dotnetcore 2.1不兼容。

ken*_*yzx 13

我尝试建立一个名为UseSerilog的新.NET Core 2.1控制台项目,我可以重现此问题。

从配置中删除DestructureFilter部分后,该应用便开始工作。

然后,我检查了Serilog.Settings.Configuration程序包的源代码,找到了commit,并得出结论,您需要编写一些代码才能使其按这种方式工作。

对于以下配置,您应该CustomPolicy在实现的“示例”名称空间中编写一个类IDestructuringPolicy

{
    "Name": "With",
    "Args": { "policy": "Sample.CustomPolicy, Sample" }
}
Run Code Online (Sandbox Code Playgroud)

如果将其删除,然后Destructure按照以下说明进行操作,则它可以正常工作。

"Destructure": [
  {
    "Name": "ToMaximumDepth",
    "Args": { "maximumDestructuringDepth": 3 }
  },
  {
    "Name": "ToMaximumStringLength",
    "Args": { "maximumStringLength": 10 }
  },
  {
    "Name": "ToMaximumCollectionCount",
    "Args": { "maximumCollectionCount": 5 }
  }
]
Run Code Online (Sandbox Code Playgroud)

希望这项发现对您有所帮助。