Oracle 数据库的 Serilog 配置

Serilog configuration for Oracle database

我在我的数据库中创建了一个 table,我想在这个 table 中存储我的日志信息。我使用了位于此处 https://github.com/lucascebertin/Serilog.Sinks.Oracle 的代码。我使用的更具体:

var connectionString =
      "user id=system;password=oracle;data source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST = localhost)(PORT = 49161)))(CONNECT_DATA=(SERVICE_NAME = xe)))";

  // If you choose to use the trigger just pass string.Empty in function name argument (tableSpaceAndFunctionName)

      var logger = new LoggerConfiguration()
          .MinimumLevel.Verbose()
          .WriteTo.Oracle(cfg => 
              cfg.WithSettings(connectionString)
              .UseBurstBatch() // or if you want to use PeriodicBatch, call .UsePeriodicBatch()
              .CreateSink())
          .CreateLogger();

如何定义 table 和我想存储日志信息的列?

WithSettings(connectionString) 调用中有一个可选参数 tableSpaceAndTableName

编辑:

var logger = new LoggerConfiguration()
          .MinimumLevel.Verbose()
          .WriteTo.Oracle(cfg => 
              cfg.WithSettings(connectionString, tableSpaceAndTableName: "MyLogTable")
              .UseBurstBatch() // or if you want to use PeriodicBatch, call .UsePeriodicBatch()
              .CreateSink())
          .CreateLogger();

编辑 2:

添加新列:

const string column = "ADDITIONALDATACOLUMN";
var columnOptions = new ColumnOptions
{
    AdditionalDataColumns = new List<DataColumn>
    {
        new DataColumn(column , typeof(string))
    }
};

并传递给WithSettings

中的columnOptions参数
new LoggerConfiguration()
            .Enrich.WithProperty("ADDITIONALDATACOLUMN", "MyValue")  /* uncomment this line if you want to store a "constant value" */
      .MinimumLevel.Verbose()
      .WriteTo.Oracle(cfg =>
          cfg.WithSettings(logConnectionString, columnOptions: columnOptions)
          .UseBurstBatch()
          .CreateSink())
      .CreateLogger();

如果您通过 Enrich.WithProperty 传递它,您可以将其添加为静态值,或者将其添加到您的日志消息中,理论上也应该根据您的用例设置该列(未测试)你需要。