ABP VnextSeriLog如何保存日志到Oracle
1、安装包"SeriLog.Sinks.Oracle(1.1.1)"
2、在Program类的主方法里添加如下配置
public static async Task< int > Main ( string [ ] args)
{
var configureBuilder = new ConfigurationBuilder ( )
. SetBasePath ( Directory. GetCurrentDirectory ( ) )
. AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
. Build ( ) ;
var connectionString = configureBuilder. GetConnectionString ( "Default" ) ;
const string column = "UserId" ;
var columnOptions = new ColumnOptions
{
AdditionalDataColumns = new List< DataColumn> { new DataColumn ( column, typeof ( string ) ) }
} ;
Log. Logger = new LoggerConfiguration ( )
#if DEBUG
. MinimumLevel. Debug ( )
#else
. MinimumLevel. Information ( )
#endif
. MinimumLevel. Override ( "Microsoft" , LogEventLevel. Information)
. MinimumLevel. Override ( "Microsoft.EntityFrameworkCore" , LogEventLevel. Warning)
. MinimumLevel. Override ( "Quartz" , LogEventLevel. Warning)
. Enrich. FromLogContext ( )
. WriteTo. Async ( c =>
{
c. Logger ( _ => _. Filter. ByIncludingOnly ( d =>
d. Level == LogEventLevel. Warning
)
. WriteTo. Oracle ( o =>
o. WithSettings ( connectionString, columnOptions : columnOptions)
. UseBurstBatch ( )
. CreateSink ( )
)
) ;
} )
. WriteTo. Async ( c => c. File ( "./Logs/logs.txt" , rollingInterval : RollingInterval. Day) )
. WriteTo. Async ( c =>
{
c. Console ( ) ;
} )
. CreateLogger ( ) ;
}
3、在Oracle里建一张表
CREATE TABLE "WMS" . "LOG" (
"Id" NUMBER NOT NULL ,
"Message" CLOB,
"MessageTemplate" CLOB,
"Level" NVARCHAR2( 128 ) ,
"TimeStamp" TIMESTAMP ( 6 ) NOT NULL ,
"Exception" CLOB,
"Properties" CLOB,
"LogEvent" CLOB,
"UserId" RAW( 16 )
)
LOGGING
NOCOMPRESS
PCTFREE 10
INITRANS 1
STORAGE (
INITIAL 65536
NEXT 1048576
MINEXTENTS 1
MAXEXTENTS 2147483645
BUFFER_POOL DEFAULT
)
PARALLEL 1
NOCACHE
DISABLE ROW MOVEMENT
;
加索引
ALTER TABLE "WMS" . "LOG" ADD CONSTRAINT "SYS_C009610" CHECK ( "Id" IS NOT NULL ) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
ALTER TABLE "WMS" . "LOG" ADD CONSTRAINT "SYS_C009611" CHECK ( "TimeStamp" IS NOT NULL ) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
好像由于oracle 11 没有自增,我还加了个触发器用来自增主键
CREATE TRIGGER "WMS" . "LOG_TRIGGER" BEFORE INSERT ON "WMS" . "LOG" REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW
BEGIN
IF :new. "Id" IS NULL THEN
SELECT
WMS. LOG_SEQUENCE. NEXTVAL
INTO
:new. "Id"
FROM dual;
END IF ;
END ;
/
4、使用
直接在代码里写就好了,ABP Vnext貌似在应用服务基类里封装了Logger属性
Loger. LogInformation ( "操作日志:阿珍爱上了阿强" )