Serilog.Sinks.Postgresql:从appsettings.json读取配置

时间:2018-06-15 11:50:39

标签: json postgresql asp.net-core configuration serilog

Serilog.Sinks.PostgreSQL接收器在asppsettings.json中有自己的配置吗?

这样的事情:

"Serilog": {
...
"WriteTo": [
{
    "Name": "PostgreSQL",
    "Args": {
        "connectionStringName": "Default",
        "tableName": "Log",
        ...
        "columnOptions": [
            {
                ...
            }
       ]
     }
 },
 ...

1 个答案:

答案 0 :(得分:1)

您可以使用Serilog.Settings.Configuration从那里添加自己的配置。 这里有一个很好的例子:github of seriog app-setting

我猜你的情况看起来像是:

 var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
                .Build();

            var logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

当您的配置看起来像:

{
  "Serilog": {
    "Using":  ["Serilog.Sinks.Postgresql"],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" } }
    ],
    "Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
    "Destructure": [
      { "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
      { "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
      { "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
      { "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
    ],
    "Properties": {
        "Application": "Sample"
    }
  }
}

您可以在此处获取更多信息gitub of Serilog setting