serilog-contrib / serilog-sinks-slackclient

Slack Sink for Serilog
Apache License 2.0
27 stars 15 forks source link

Provide example renderer overload in appsettings configuration #17

Open Indrego opened 5 years ago

Indrego commented 5 years ago

In the readme it shows how to provide a custom renderer when configuring the sink in code:

var log = new LoggerConfiguration()
    .WriteTo.Slack("https://hooks.slack.com/services/T000/B000/XXXX",
                MyApp.MyRenderer.RenderMessage)
    .CreateLogger();

However, I'm struggling to do the same using the serilog configuration extension:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Slack.Core" ],
    "MinimumLevel": {
      "Default": "Debug"
    },
    "WriteTo": [
      {
        "Name": "Slack",
        "Args": {
          "webhookUri": "https://hooks.slack.com/services/T000/B000/XXXX",
          "restrictedToMinimumLevel": "Information",
          "renderMessageImplementation": "MyApp.MyRenderer::RenderMessage, MyApp"
        }
      }
    ]
  }
}
Unhandled Exception: System.InvalidCastException: Invalid cast from 'System.String' to 'Serilog.Sinks.Slack.Core.SlackSink+RenderMessageMethod'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at Serilog.Settings.Configuration.StringArgumentValue.ConvertTo(Type toType, IReadOnlyDictionary`2 declaredLevelSwitches)
   at Serilog.Settings.Configuration.ConfigurationReader.<>c__DisplayClass20_0.<CallConfigurationMethods>b__3(<>f__AnonymousType7`2 <>h__TransparentIdentifier0)

What is the correct syntax of renderMessageImplementation in the appsettings.json to do this? I tried looking at the ConvertTo code but am not sure what I'm missing.