From e20129a4c1e366d5b6c7d2b9f8f03876cd7424a6 Mon Sep 17 00:00:00 2001 From: huangxianguo Date: Mon, 9 Sep 2024 11:52:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90Serilog=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MasstransferCommon/Config/LogConfiguration.cs | 60 +++++++++++++++++++ MasstransferCommon/MasstransferCommon.csproj | 5 ++ .../MasstransferExporter.csproj | 1 + MasstransferExporter/Program.cs | 17 +++--- .../MasstransferInfrastructure.csproj | 1 + 5 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 MasstransferCommon/Config/LogConfiguration.cs diff --git a/MasstransferCommon/Config/LogConfiguration.cs b/MasstransferCommon/Config/LogConfiguration.cs new file mode 100644 index 0000000..7d4fe71 --- /dev/null +++ b/MasstransferCommon/Config/LogConfiguration.cs @@ -0,0 +1,60 @@ +using Serilog; +using Serilog.Core; +using Serilog.Events; + +namespace MasstransferCommon.Config; + +/// +/// 日志配置类 +/// +public class LogConfiguration +{ + /// + /// 获取日志配置对象 + /// + /// + 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(); + } +} \ No newline at end of file diff --git a/MasstransferCommon/MasstransferCommon.csproj b/MasstransferCommon/MasstransferCommon.csproj index a9ad36d..db835be 100644 --- a/MasstransferCommon/MasstransferCommon.csproj +++ b/MasstransferCommon/MasstransferCommon.csproj @@ -12,6 +12,11 @@ + + + + + diff --git a/MasstransferExporter/MasstransferExporter.csproj b/MasstransferExporter/MasstransferExporter.csproj index 0ef7439..960753e 100644 --- a/MasstransferExporter/MasstransferExporter.csproj +++ b/MasstransferExporter/MasstransferExporter.csproj @@ -13,6 +13,7 @@ + diff --git a/MasstransferExporter/Program.cs b/MasstransferExporter/Program.cs index ba1ab6b..8faff95 100644 --- a/MasstransferExporter/Program.cs +++ b/MasstransferExporter/Program.cs @@ -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("select * from mqtt_params").FirstOrDefault(); // 启动mqtt连接 @@ -37,8 +38,8 @@ class Program // 启动完成后,广播启动通知 EventBus.Publish(EventType.StartUp, true); - DelayScheduler.Delay(async () => ImageService.ImageExporter(), - TimeSpan.FromSeconds(5)); + // DelayScheduler.Delay(async () => ImageService.ImageExporter(), + // TimeSpan.FromSeconds(5)); // 启动与主程序的通信 // ProcessHelper.Init(); diff --git a/MasstransferInfrastructure/MasstransferInfrastructure.csproj b/MasstransferInfrastructure/MasstransferInfrastructure.csproj index 83d60ab..278f22c 100644 --- a/MasstransferInfrastructure/MasstransferInfrastructure.csproj +++ b/MasstransferInfrastructure/MasstransferInfrastructure.csproj @@ -18,6 +18,7 @@ +