集成Serilog日志框架

This commit is contained in:
huangxianguo 2024-09-09 11:52:15 +08:00
parent 4dd680b0dc
commit e20129a4c1
5 changed files with 76 additions and 8 deletions

View File

@ -0,0 +1,60 @@
using Serilog;
using Serilog.Core;
using Serilog.Events;
namespace MasstransferCommon.Config;
/// <summary>
/// 日志配置类
/// </summary>
public class LogConfiguration
{
/// <summary>
/// 获取日志配置对象
/// </summary>
/// <returns></returns>
public static Logger GetLogger()
{
// 日志输出目录
var basePath = AppDomain.CurrentDomain.BaseDirectory;
return new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Logger(
l =>
l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug)
.WriteTo.File(
Path.Combine(basePath, "logs", "debug", "debug-.log"),
rollingInterval: RollingInterval.Hour,
retainedFileCountLimit: 24
)
)
.WriteTo.Logger(
l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information)
.WriteTo.File(
Path.Combine(basePath, "logs", "info", "info-.log"),
rollingInterval: RollingInterval.Hour,
retainedFileCountLimit: 72
)
)
.WriteTo.Logger(
l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning)
.WriteTo.File(
Path.Combine(basePath, "logs", "warning", "warning-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 30
)
)
.WriteTo.Logger(
l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.File(
Path.Combine(basePath, "logs", "error", "error-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 90
)
)
.WriteTo.Console()
.CreateLogger();
}
}

View File

@ -12,6 +12,11 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.15" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="Quartz" Version="3.10.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.1-dev-00077" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
<PackageReference Include="sqlite-net-sqlcipher" Version="1.9.172" />
<PackageReference Include="System.Management" Version="8.0.0"/>
<PackageReference Include="Serilog" Version="4.0.0"/>

View File

@ -13,6 +13,7 @@
<PackageReference Include="M2Mqtt" Version="4.3.0" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.6.1152" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.1-dev-00077" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />

View File

@ -1,14 +1,11 @@
using MasstransferCommon.Events;
using MasstransferCommon.Config;
using MasstransferCommon.Events;
using MasstransferCommon.Model.Entity;
using MasstransferCommon.Scheduler;
using MasstransferCommunicate.Mqtt.Client;
using MasstransferCommunicate.Process.Client;
using MasstransferExporter.DataExporter;
using MasstransferExporter.ImageExporter;
using MasstransferExporter.Init;
using MasstransferExporter.Stat;
using MasstransferInfrastructure.Database.Sqlite;
using MasstransferInfrastructure.Mqtt.Model;
using Serilog;
namespace MasstransferExporter;
@ -21,6 +18,10 @@ class Program
// 进行初始化调用
InstantUtil.Init();
Log.Logger = LogConfiguration.GetLogger();
Log.Error("Masstransfer Exporter Startup");
var mqttParams = Db.Query<MqttParams>("select * from mqtt_params").FirstOrDefault();
// 启动mqtt连接
@ -37,8 +38,8 @@ class Program
// 启动完成后,广播启动通知
EventBus<bool>.Publish(EventType.StartUp, true);
DelayScheduler.Delay(async () => ImageService.ImageExporter(),
TimeSpan.FromSeconds(5));
// DelayScheduler.Delay(async () => ImageService.ImageExporter(),
// TimeSpan.FromSeconds(5));
// 启动与主程序的通信
// ProcessHelper.Init();

View File

@ -18,6 +18,7 @@
<PackageReference Include="MQTTnet" Version="4.3.6.1152"/>
<PackageReference Include="Serilog" Version="4.0.0"/>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0-preview.2.24128.4"/>
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.1-dev-00077" />
<PackageReference Include="sqlite-net" Version="1.6.292"/>
<PackageReference Include="sqlite-net-sqlcipher" Version="1.9.172"/>
<PackageReference Include="Stateless" Version="5.15.0"/>