Serilog Sinks:从 json 配置中读取无效

Serliog Sinks: Reading from json configuration not working

系统未在我的 postgres 数据库中自动创建日志 table 和记录错误。我正在使用 Serilog.Sinks.PostgresSQL。我错过了什么?

Program.cs:

public class Program
  {
    public static void Main(string[] args)
    {
      var path = Directory.GetCurrentDirectory();
      var environmentName =Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

      var configuration = new ConfigurationBuilder()
        .SetBasePath(path)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
      Log.Logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();
      try
      {
        var iWebHost = CreateWebHostBuilder(args).Build();
        iWebHost.Run();
      }
      finally
      {
        Log.CloseAndFlush();
      }
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseSerilog();
  }

appsettings.Development.json

"Serilog": {
    "Using": [ "Serilog.Sinks.PostgreSQL" ],
    "MinimumLevel": "Warning",
    "WriteTo": [
      {
        "Name": "PostgreSQL",
        "Args": {
          "connectionString": "Server=localhost;Port=5432;Database=postgres;User Id=postgres;Password=<password>;Pooling=true;Minimum Pool Size=1;Maximum Pool Size=100;",
          "tableName": "Logs",
          "autoCreateSqlTable": true,
          "batchPostingLimit": 1 
        }
      }
    ]
  }

这样试试:

WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(hostingContext.Configuration))
            .Build();

你的问题好像是自动生成的参数名table不正确。尝试更改如下参数:

"Serilog": {
"Using": [ "Serilog.Sinks.PostgreSQL" ],
"MinimumLevel": "Warning",
"WriteTo": [
  {
    "Name": "PostgreSQL",
    "Args": {
      "connectionString": "Server=localhost;Port=5432;Database=postgres;User Id=postgres;Password=<password>;Pooling=true;Minimum Pool Size=1;Maximum Pool Size=100;",
      "tableName": "Logs",
      "needAutoCreateTable": true,
      "batchPostingLimit": 1
    }
  }
] 
}

参考:https://github.com/b00ted/serilog-sinks-postgresql

已更新 :

你可以在Configure方法中添加Serilog.Debugging.SelfLog.Enable来获取Serilog自身产生的异常,参考下面的代码:

Serilog.Debugging.SelfLog.Enable(msg =>
{
            Debug.Print(msg);
            Debugger.Break();
});

Here是我的demo,如果还有错误,可以和你的demo对比一下。

我也在找这个,发现了on the project's github issue page 您需要在“columnOptionsSection”中使用“removeStandardColumns”属性。

    WriteTo": [
{
    "Name": "PostgreSQL",
    "Args": {
        "connectionString": "User ID=;Password=;Host=localhost;Port=5432;Database=",
        "needAutoCreateTable": "true",
        "schemaName": "banklink",
        "tableName": "Logs",
        "columnOptionsSection": {
            "removeStandardColumns": [
                "Message",
                "Message_Tempate",
                "Level",
                "Timestamp",
                "Exception",
                "Log_Event"
            ],
            "customColumns": [
                {
                    "ColumnName": "message",
                    "DataType": "text",
                    "AllowNull": true
                },
                {
                    "ColumnName": "message_template",
                    "DataType": "text",
                    "AllowNull": true
                },
                {
                    "ColumnName": "logcode",
                    "DataType": "integer",
                    "AllowNull": true
                },
                {
                    "ColumnName": "level",
                    "DataType": "integer",
                    "AllowNull": true
                },
                {
                    "ColumnName": "timestamp",
                    "DataType": "timestamp",
                    "AllowNull": true
                },
            }
        }
    }
}