【问题标题】:Invalid cast from 'System.String' to 'Serilog.Core.IDestructuringPolicy'从“System.String”到“Serilog.Core.IDestructuringPolicy”的无效转换
【发布时间】:2018-11-02 03:33:33
【问题描述】:

从学习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!");

在 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.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
   

我决定尝试将 serilog-settings-configuration 示例项目升级到 .netcore2.1 并因此询问this question

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

【问题讨论】:

标签: c# .net-core serilog


【解决方案1】:

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

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

然后我检查了Serilog.Settings.Configuration包的源代码,我找到了this commit,我得出的结论是你需要编写一些代码才能让它像这样工作。

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

{
    "Name": "With",
    "Args": { "policy": "Sample.CustomPolicy, Sample" }
}

如果你删除它,并留下Destructure 如下,它就可以工作了。

"Destructure": [
  {
    "Name": "ToMaximumDepth",
    "Args": { "maximumDestructuringDepth": 3 }
  },
  {
    "Name": "ToMaximumStringLength",
    "Args": { "maximumStringLength": 10 }
  },
  {
    "Name": "ToMaximumCollectionCount",
    "Args": { "maximumCollectionCount": 5 }
  }
]

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

【讨论】:

  • 谢谢。它也适用于 CustomPolicy,一旦我意识到我必须编写它。
猜你喜欢
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
相关资源
最近更新 更多