Invalid cast from 'System.String' to 'Serilog.Core.IDestructuringPolicy'

6

From studying Serilog.Sinks.AzureTableStorage I have the following

In 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!");

In appsetttings.json ( with different connection string)

"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"
    }    
  }

I see in the debug output yet no data gets logged to the storage 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'.

[Update]

If I use a very simple config as per the ReadMe.Md in GitHub, I get the same error.

[Update]

I copied in the code from Kennyzx link. The error has changed to

System.InvalidCastException
  HResult=0x80004002
  Message=Invalid cast from 'System.String' to 'Serilog.Core.ILogEventFilter'.
  Source=System.Private.CoreLib

I decided to try upgrading the serilog-settings-configuration sample project to .netcore2.1 and thus asked this question

After a few hours I came to the conclusion that serilog-settings-configuration s is not compatible with dotnetcore 2.1

c#
.net-core
serilog
asked on Stack Overflow Nov 2, 2018 by Kirsten Greed • edited Nov 3, 2018 by Kirsten Greed

1 Answer

12

I try to set up a new .NET Core 2.1 Console project called UseSerilog and I can reproduce the issue.

After I remove the Destructure and Filter sections from the configuration then the app begins to work.

Then I check the source code of the Serilog.Settings.Configuration package, I find this commit, and I come to conclusion that you need to write some code in order to make it work like this way.

For the below configuration, you are expected to write a CustomPolicy class in the "Sample" namespace that implements IDestructuringPolicy.

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

If you remove this, and leave Destructure as below, it is working.

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

Hope this finding helps you.

answered on Stack Overflow Nov 3, 2018 by kennyzx

User contributions licensed under CC BY-SA 3.0