如何在appsettings.json中配置serilog-sinks-slack

时间:2019-05-14 14:20:30

标签: c# asp.net-core-webapi serilog

我正在尝试在asp.net核心应用程序上配置serilog,我可以使用启动配置进行设置,但不能在appsettings.json上进行设置。 当我这样做时:

我正在使用此水槽:

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);

它工作正常,我可以在我的闲暇频道中看到消息。 然后,我尝试将其添加到我的应用程序设置中,因此将其更改为:

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

以及appconfig:

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

但是它不会在松弛状态下写任何东西。我也尝试过:

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

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

任何提示我的配置/设置都用完了吗?

1 个答案:

答案 0 :(得分:0)

啊!我应该使用Args代替评论中提到的 Hugo 的选项

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