.Net 6使用Serilog sink功能登录到Oracle数据库

qvk1mo1f  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(216)

我正在尝试使用serilog将信息和错误记录到oracle数据库中
我只看到MS SQL Server的例子。我用它作为例子创建了这个配置。我没有看到任何关于Oracle选项的博客或信息。
请帮
这里是配置文件和程序文件。
Appsetting.json

"Serilog": {
  "Using": [ "Serilog.Sinks.Oracle" ],
  "MinimumLevel": {
    "Default": "Information",
    "Override": {
      "Microsoft": "Error",
      "Microsoft.AspNetCore.Mvc": "Warning"
    }
  },
"WriteTo": [
  {
    "Name": "Oracle",
    "Args": {
      "connectionString": "xxx",
      "tableName": "Log",
"autoCreateSqlTable": false,
"columnOptionSection": {
  "removeStandartColumns": [ "Id", "Message", "MessageTemplate", "Level", "TimeStamp", "Exception", "Properties" ],
  "customColumns": [
{
"ColumnName": "Application",
  "DataType": "nvarchar",
  "DataLength": "10"
},
{
  "ColumnName": "Source",
  "DataType": "nvarchar",
  "DataLength": "10"
},
{
  "ColumnName": "Message_TXT",
  "DataType": "nvarchar",
  "DataLength": "4000"
},
{
  "ColumnName": "TimeStamp_Dt",
  "DataType": "date"
},
]
}
}
}

字符串
Program.cs

var configuration = new ConfigurationBuilder()
      .SetBasePath(Directory.GetCurrentDirectory())
      .AddJsonFile("appsettings.json")
      .Build();

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

LogContext.PushProperty("Application", "TEST");
LogContext.PushProperty("Source_Txt", "SERILOG");
LogContext.PushProperty("Message_txt", message);
LogContext.PushProperty("TimeStamp_DT", DateTime.Now);

logger.Information(message);


它不会抛出任何错误,也不会将任何信息记录到数据库中。

yws3nbqq

yws3nbqq1#

这是一个实验代码,请使用它对您自己的风险。
您需要使用下面的脚本手动创建表。我已经遵循this作为参考。

CREATE TABLE MY_TABLE_SPACE.SERILOG(
  "Id"                 INT             NOT NULL ENABLE,
  "Message"            CLOB            NULL,
  "MessageTemplate"    CLOB            NULL,
  "Level"              NVARCHAR2(128)  NULL,
  "EventType"          NVARCHAR2(128)  NULL,
  "TimeStamp"          TIMESTAMP       NOT NULL,
  "Exception"          CLOB            NULL,
  "Properties"         CLOB            NULL,
  "LogEvent"           CLOB            NULL
);
-- You will need a sequence to get next id
CREATE SEQUENCE MY_TABLE_SPACE.SERILOG_SEQUENCE START WITH 1 INCREMENT BY 1;
   

-- Create a function to get next id
CREATE FUNCTION MY_TABLE_SPACE.GET_NEXT_SERILOG_SEQ RETURN INT IS
BEGIN
  RETURN MY_TABLE_SPACE.SERILOG_SEQUENCE.NEXTVAL;
END;

字符串
代码示例(这是一个实验性的代码,你自己使用它的风险!!)

var connString =
      "Your Oracle DB Connection String";
 
  const string column = "CUSTOMCOLUMN";
  var columnOptions = new ColumnOptions
  {
      AdditionalDataColumns = new List<DataColumn>
      {
          new DataColumn(column , typeof(string))
      }
  };

  Log.Logger = new LoggerConfiguration()
     
      .MinimumLevel.Verbose()
      .WriteTo.Oracle(cfg =>
          cfg.WithSettings(logConnectionString, columnOptions: columnOptions)
          .UseBurstBatch()
          .CreateSink())
      .CreateLogger();

  logger.Debug("Logging Serilog Output to Oracle table");

相关问题