提问人:Miriam F. 提问时间:11/16/2023 更新时间:11/16/2023 访问量:13
Serilog.Sinks.Oracle - 使用 C# .Net Framework 中的 Serilog 将日志写入 Oracle DB
Serilog.Sinks.Oracle - Write Log to Oracle DB with Serilog from C# .Net Framework
问:
我想从我的程序 (.Net Framework) 将日志写入控制台、文件和 Oracle DB。 我能够将日志写入控制台和文件。 而且我无法写入数据库。 我的代码:
{
public static readonly ILogger _log;
static SerilogClass()
{
string logsFolder = ConfigurationManager.AppSettings["logsFolder"];
string environment = ConfigurationManager.AppSettings["environment"];
string connection = ConfigurationManager.AppSettings["ConnectionString"];
_log = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File(
path: Path.Combine(logsFolder, $"{DateTime.Now:yyyy-MM-dd_}.log"),
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:Ij}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: null,
shared: true)
.WriteTo.Oracle(connection, "Logs")
.CreateLogger();
}
}
如何将日志写入 Oracle DB? 我很乐意提供建议。谢谢。
答:
0赞
Chinmay T
11/20/2023
#1
您需要安装此软件包,并且可以从此处获取有关它的信息。这仍处于早期开发阶段,因此使用此代码的风险由您自己承担。Serilog.Sinks.Oracle
数据库脚本:
CREATE TABLE YOUR_TABLE_SPACE.LOG(
"Id" INT NOT NULL ENABLE,
"Message" CLOB NULL,
"MessageTemplate" CLOB NULL,
"Level" NVARCHAR2(128) NULL,
"TimeStamp" TIMESTAMP NOT NULL,
"Exception" CLOB NULL,
"Properties" CLOB NULL,
"LogEvent" CLOB NULL
);
CREATE SEQUENCE YOUR_TABLE_SPACE.LOG_SEQUENCE START WITH 1 INCREMENT BY 1;
-- YOU CAN CHOOSE TO USE FUNCTION OR CREATE A TRIGGER... I PREFER THE TRIGGER WAY :)
-- TRIGGER
CREATE TRIGGER
YOUR_TABLE_SPACE.LOG_TRIGGER
BEFORE INSERT ON
YOUR_TABLE_SPACE.LOG
REFERENCING
NEW AS NEW
OLD AS old
FOR EACH ROW
BEGIN
IF :new."Id" IS NULL THEN
SELECT
YOUR_TABLE_SPACE.LOG_SEQUENCE.NEXTVAL
INTO
:new."Id"
FROM dual;
END IF;
END;
-- OR FUNCTION
CREATE FUNCTION YOUR_TABLE_SPACE.get_seq RETURN INT IS
BEGIN
RETURN YOUR_TABLE_SPACE.LOG_SEQUENCE.NEXTVAL;
END;
如何在 .net 应用程序中使用:
//Don't forget to add the namespace, ok?
var connectionString =
"Your Oracle DB conn string";
// 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();
const string column = "ADDITIONALDATACOLUMN";
var columnOptions = new ColumnOptions
{
AdditionalDataColumns = new List<DataColumn>
{
new DataColumn(column , typeof(string))
}
};
Log.Logger = new LoggerConfiguration()
//.Enrich.FromLogContext() /* uncomment this line if you want to store dynamic values and passing them by LogContext.PushProperty(name, value)... remember, this PushProperty is Disposable*/
//.Enrich.WithProperty("column", "constant value, lika machine's hostname") /* uncomment this line if you want to store a "constant value" */
.MinimumLevel.Verbose()
.WriteTo.Oracle(cfg =>
cfg.WithSettings(logConnectionString, columnOptions: columnOptions)
.UseBurstBatch()
.CreateSink())
.CreateLogger();
//Be aware of the batch limit and delay time configured up here!
logger.Debug("Yey, this message will be stored on Oracle!!");
评论