serilog-settings-configuration icon indicating copy to clipboard operation
serilog-settings-configuration copied to clipboard

Constructor parameters - support objects with collection properties

Open sfkoch opened this issue 2 years ago • 0 comments

I've been trying to configure Serilog.Sinks.Splunk.EventCollectorSink from appsettings.json, but can't get it to construct the CustomFields object for the "fields" parameter. When I provide an array of 3 CustomField objects, TryBuildCtorExpression is trying and failing to find a constructor for List<CustomField> that takes 3 arguments.

This is the JSON I've tried:

    "WriteTo:EventCollector": {
      "Name": "EventCollector",
      "Args": {
        "splunkHost": "redacted",
        "eventCollectorToken": "redacted",
        "restrictedToMinimumLevel": "Warning",
        "host": "%COMPUTERNAME%",
        "fields": {
          "$type": "Serilog.Sinks.Splunk.CustomFields, Serilog.Sinks.Splunk",
          "customFields": [
            {
              "$type": "Serilog.Sinks.Splunk.CustomField, Serilog.Sinks.Splunk",
              "name": "name1",
              "value": "value1"
            },
            {
              "$type": "Serilog.Sinks.Splunk.CustomField, Serilog.Sinks.Splunk",
              "name": "name2",
              "value": "value2"
            },
            {
              "$type": "Serilog.Sinks.Splunk.CustomField, Serilog.Sinks.Splunk",
              "name": "name3",
              "value": "value3"
            }
          ]
        }
      }
    }

I also tried structuring "fields" for the fallback to ConfigurationBinding.Get in ObjectArgumentValue.ConvertTo, but that failed because CustomField doesn't have a default constructor.

Perhaps TryBuildCtorExpression needs something like TryCreateContainer from ConvertTo for when parameterType is a collection?

sfkoch avatar May 12 '22 20:05 sfkoch