Search code examples
c#asp.net-core-webapiserilog

How do I configure serilog-sinks-slack in appsettings.json


I am trying to configure serilog on my asp.net core application and I can set it up using the startup configuration, but I cannot do it on appsettings.json. When I do:

I am using this sink:

https://github.com/mgibas/serilog-sinks-slack

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
Logger log = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .WriteTo.Slack(new SlackSinkOptions()
                {
                    WebHookUrl = "https://hooks.slack.com/services/xxx/yyy/zzz",
                    CustomChannel = "@myuser"
                }).CreateLogger();
    loggerFactory.AddSerilog(log);

It works fine and I can see the messages in my slack channel. Then I try to bring it in my app settings so I change it to this:

Logger log = new LoggerConfiguration()
.MinimumLevel.Verbose()
.ReadFrom.Configuration(Configuration)
.CreateLogger();

and with the appconfig:

{
  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Slack",
        "SlackSinkOptions":
        {
          "WebHookUrl": "https://hooks.slack.com/services/xxx/yyy/zzz",
          "CustomChannel": "@myuser"
        }
      }
    ]
  },
  "AllowedHosts": "*"
}

But it doesn't write anything in slack. I've also tried:

{
  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Slack",
        "WebHookUrl": "https://hooks.slack.com/services/xxx/yyy/zzz",
        "CustomChannel": "@myuser"

      }
    ]
  },
  "AllowedHosts": "*"
}

Any clue what is worng with my config/setup?


Solution

  • Ah! I should have used Args instead of Options as Hugo mentioned in the comments

    {
      "Serilog": {
        "MinimumLevel": "Debug",
        "WriteTo": [
          {
            "Name": "Slack",
            "Args":
            {
              "WebHookUrl": "https://hooks.slack.com/services/xxx/yyy/zzz",
              "CustomChannel": "@myuser"
            }
          }
        ]
      },
      "AllowedHosts": "*"
    }