简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM