Abp6.0中已经启用Serilog,使用Serilog.Sinks.MariaDB包可以保存到MariaDB,mysql中
一种做法是在var loggerConfiguration = new LoggerConfiguration( )后使用WriteTo.MariaDB扩展方法来配置,这样在代码中配置不够灵活,修改起来也不方便
其实在Serilog.Sinks.MariaDB的github上很直接的提供了另外一种方式,通过配置文件来配置。这里需要使用*Serilog.Settings.Configuration*来读取配置文件。关于配置,其实是有默认配置形式的。短短几行代码就解决了问题,
//生成一个ConfigurationBuilder,把配置文件加进来
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
.Build();
var loggerConfiguration = new LoggerConfiguration( )
.ReadFrom.Configuration(configuration)//从文件中读取配置
配置文件的格式如下,具体含义请找官方文档:
"Serilog": {
"Using": [
"Serilog.Sinks.MariaDB"
],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "MariaDB",
"Args": {
"connectionString": "Server=192.168.1.1;Port=12345;Database=xxxx;Uid=xxx;Pwd=xxxxxxxxx;",
"autoCreateTable": true,
"tableName": "Logs",
"restrictedToMinimumLevel": "Warning",
"batchPostingLimit": 1000,
"period": "0.00:00:30",
"options": {
"PropertiesToColumnsMapping": {
"Exception": "Exception",
"Level": "Level",
"Message": "Message",
"MessageTemplate": "MessageTemplate",
"Properties": "Properties",
"Timestamp": "Timestamp"
},
"TimestampInUtc": true,
"ExcludePropertiesWithDedicatedColumn": true,
"EnumsAsInts": true,
"LogRecordsCleanupFrequency": "0.02:00:00",
"LogRecordsExpiration": "31.00:00:00"
}
}
}
]
}
我们需要创造,也需要借鉴,毕竟站在巨人的肩膀上才会更高