From ad719d157b7fc5e6a52d7cd9ee9a734e9f2bb8e0 Mon Sep 17 00:00:00 2001 From: QI Mingxuan Date: Fri, 15 May 2026 15:49:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=20Dump=E3=80=81Logging?= =?UTF-8?q?=E3=80=81Database=20=E7=AD=89=E5=8A=9F=E8=83=BD=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=8A=A0=E8=BD=BD=E5=92=8C?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=B1=BB=EF=BC=8C=E4=BC=98=E5=8C=96ConfigLoa?= =?UTF-8?q?der=EF=BC=8C=E8=81=8C=E8=B4=A3=E5=88=86=E6=95=A3=E5=90=84?= =?UTF-8?q?=E4=B8=AA=E6=A8=A1=E5=9D=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XP.Common/Database/Configs/ConfigLoader.cs | 46 ++++++++++++++++ XP.Common/Database/Configs/SqliteConfig.cs | 52 +++++++++++++++++++ .../Database/Implementations/SqliteContext.cs | 2 +- XP.Common/Dump/Configs/ConfigLoader.cs | 46 ++++++++++++++++ XP.Common/Logging/Configs/ConfigLoader.cs | 50 ++++++++++++++++++ XP.Common/Logging/Configs/SerilogConfig.cs | 43 +++++++++++++++ XP.Common/Logging/SerilogInitializer.cs | 2 +- 7 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 XP.Common/Database/Configs/ConfigLoader.cs create mode 100644 XP.Common/Database/Configs/SqliteConfig.cs create mode 100644 XP.Common/Dump/Configs/ConfigLoader.cs create mode 100644 XP.Common/Logging/Configs/ConfigLoader.cs create mode 100644 XP.Common/Logging/Configs/SerilogConfig.cs diff --git a/XP.Common/Database/Configs/ConfigLoader.cs b/XP.Common/Database/Configs/ConfigLoader.cs new file mode 100644 index 0000000..37289c5 --- /dev/null +++ b/XP.Common/Database/Configs/ConfigLoader.cs @@ -0,0 +1,46 @@ +using System.Configuration; + +namespace XP.Common.Database.Configs +{ + /// + /// SQLite 配置加载器,从 App.config 读取数据库相关配置项 | SQLite configuration loader, reads database-related configuration from App.config + /// + public static class ConfigLoader + { + /// + /// 配置键前缀 | Configuration key prefix + /// + private const string KeyPrefix = "Sqlite:"; + + /// + /// 从 App.config 加载 SQLite 配置 | Load SQLite configuration from App.config + /// + /// SQLite 配置实体,缺失或无效配置项使用默认值 | SQLite configuration entity, uses default values for missing or invalid items + public static SqliteConfig LoadSqliteConfig() + { + var config = new SqliteConfig(); + + // 加载数据库文件路径 | Load database file path + var dbPath = ConfigurationManager.AppSettings[KeyPrefix + "DbFilePath"]; + if (!string.IsNullOrEmpty(dbPath)) config.DbFilePath = dbPath; + + // 加载连接超时时间 | Load connection timeout + var timeout = ConfigurationManager.AppSettings[KeyPrefix + "ConnectionTimeout"]; + if (int.TryParse(timeout, out var t) && t > 0) config.ConnectionTimeout = t; + + // 加载是否自动创建 | Load create if not exists + var createIfNotExists = ConfigurationManager.AppSettings[KeyPrefix + "CreateIfNotExists"]; + if (bool.TryParse(createIfNotExists, out var c)) config.CreateIfNotExists = c; + + // 加载是否启用 WAL 模式 | Load enable WAL mode + var enableWal = ConfigurationManager.AppSettings[KeyPrefix + "EnableWalMode"]; + if (bool.TryParse(enableWal, out var w)) config.EnableWalMode = w; + + // 加载是否开启 SQL 日志 | Load enable SQL logging + var enableSqlLog = ConfigurationManager.AppSettings[KeyPrefix + "EnableSqlLogging"]; + if (bool.TryParse(enableSqlLog, out var l)) config.EnableSqlLogging = l; + + return config; + } + } +} diff --git a/XP.Common/Database/Configs/SqliteConfig.cs b/XP.Common/Database/Configs/SqliteConfig.cs new file mode 100644 index 0000000..3fdf579 --- /dev/null +++ b/XP.Common/Database/Configs/SqliteConfig.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; + +namespace XP.Common.Database.Configs +{ + /// + /// SQLite 配置实体(从 App.config 读取)| SQLite configuration entity (loaded from App.config) + /// + public class SqliteConfig + { + /// + /// 数据库文件路径 | Database file path + /// + public string DbFilePath { get; set; } = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "Files", "Data", "XP.db"); + + /// + /// 连接超时时间(秒,默认30)| Connection timeout (seconds, default 30) + /// + public int ConnectionTimeout { get; set; } = 30; + + /// + /// 数据库不存在时是否自动创建(默认true)| Whether to auto-create if not exists (default true) + /// + public bool CreateIfNotExists { get; set; } = true; + + /// + /// 是否启用 WAL 模式(提升并发性能,默认true)| Whether to enable WAL mode (default true) + /// + public bool EnableWalMode { get; set; } = true; + + /// + /// 是否开启日志记录(记录所有SQL操作,默认false)| Whether to enable SQL logging (default false) + /// + public bool EnableSqlLogging { get; set; } = false; + + /// + /// 获取 SQLite 连接字符串 | Get SQLite connection string + /// + public string GetConnectionString() + { + var builder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder + { + DataSource = DbFilePath, + Cache = Microsoft.Data.Sqlite.SqliteCacheMode.Default, + DefaultTimeout = ConnectionTimeout + }; + return builder.ToString(); + } + } +} diff --git a/XP.Common/Database/Implementations/SqliteContext.cs b/XP.Common/Database/Implementations/SqliteContext.cs index e77cc05..dd45188 100644 --- a/XP.Common/Database/Implementations/SqliteContext.cs +++ b/XP.Common/Database/Implementations/SqliteContext.cs @@ -7,7 +7,7 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using XP.Common.Configs; +using XP.Common.Database.Configs; using XP.Common.Database.Interfaces; using XP.Common.Database.Models; using XP.Common.Helpers; diff --git a/XP.Common/Dump/Configs/ConfigLoader.cs b/XP.Common/Dump/Configs/ConfigLoader.cs new file mode 100644 index 0000000..5adec98 --- /dev/null +++ b/XP.Common/Dump/Configs/ConfigLoader.cs @@ -0,0 +1,46 @@ +using System.Configuration; + +namespace XP.Common.Dump.Configs +{ + /// + /// Dump 配置加载器,从 App.config 读取 Dump 相关配置项 | Dump configuration loader, reads dump-related configuration from App.config + /// + public static class ConfigLoader + { + /// + /// 配置键前缀 | Configuration key prefix + /// + private const string KeyPrefix = "Dump:"; + + /// + /// 从 App.config 加载 Dump 配置 | Load Dump configuration from App.config + /// + /// Dump 配置实体,缺失或无效配置项使用默认值 | Dump configuration entity, uses default values for missing or invalid items + public static DumpConfig LoadDumpConfig() + { + var config = new DumpConfig(); + + // 加载存储路径 | Load storage path + var storagePath = ConfigurationManager.AppSettings[KeyPrefix + "StoragePath"]; + if (!string.IsNullOrEmpty(storagePath)) config.StoragePath = storagePath; + + // 加载是否启用定时触发 | Load enable scheduled dump + var enableScheduled = ConfigurationManager.AppSettings[KeyPrefix + "EnableScheduledDump"]; + if (bool.TryParse(enableScheduled, out var enabled)) config.EnableScheduledDump = enabled; + + // 加载定时触发间隔 | Load scheduled interval + var interval = ConfigurationManager.AppSettings[KeyPrefix + "ScheduledIntervalMinutes"]; + if (int.TryParse(interval, out var min) && min > 0) config.ScheduledIntervalMinutes = min; + + // 加载 Mini Dump 文件大小上限 | Load Mini Dump size limit + var sizeLimit = ConfigurationManager.AppSettings[KeyPrefix + "MiniDumpSizeLimitMB"]; + if (long.TryParse(sizeLimit, out var size) && size > 0) config.MiniDumpSizeLimitMB = size; + + // 加载文件保留天数 | Load retention days + var retentionDays = ConfigurationManager.AppSettings[KeyPrefix + "RetentionDays"]; + if (int.TryParse(retentionDays, out var days) && days > 0) config.RetentionDays = days; + + return config; + } + } +} diff --git a/XP.Common/Logging/Configs/ConfigLoader.cs b/XP.Common/Logging/Configs/ConfigLoader.cs new file mode 100644 index 0000000..a35442b --- /dev/null +++ b/XP.Common/Logging/Configs/ConfigLoader.cs @@ -0,0 +1,50 @@ +using System.Configuration; + +namespace XP.Common.Logging.Configs +{ + /// + /// Serilog 配置加载器,从 App.config 读取日志相关配置项 | Serilog configuration loader, reads logging-related configuration from App.config + /// + public static class ConfigLoader + { + /// + /// 配置键前缀 | Configuration key prefix + /// + private const string KeyPrefix = "Serilog:"; + + /// + /// 从 App.config 加载 Serilog 配置 | Load Serilog configuration from App.config + /// + /// Serilog 配置实体,缺失或无效配置项使用默认值 | Serilog configuration entity, uses default values for missing or invalid items + public static SerilogConfig LoadSerilogConfig() + { + var config = new SerilogConfig(); + + // 加载日志路径 | Load log path + var logPath = ConfigurationManager.AppSettings[KeyPrefix + "LogPath"]; + if (!string.IsNullOrEmpty(logPath)) config.LogPath = logPath; + + // 加载最低日志级别 | Load minimum level + var minLevel = ConfigurationManager.AppSettings[KeyPrefix + "MinimumLevel"]; + if (!string.IsNullOrEmpty(minLevel)) config.MinimumLevel = minLevel; + + // 加载是否输出到控制台 | Load enable console + var enableConsole = ConfigurationManager.AppSettings[KeyPrefix + "EnableConsole"]; + if (bool.TryParse(enableConsole, out var console)) config.EnableConsole = console; + + // 加载日志文件分割规则 | Load rolling interval + var rollingInterval = ConfigurationManager.AppSettings[KeyPrefix + "RollingInterval"]; + if (!string.IsNullOrEmpty(rollingInterval)) config.RollingInterval = rollingInterval; + + // 加载单个日志文件最大大小 | Load file size limit + var fileSize = ConfigurationManager.AppSettings[KeyPrefix + "FileSizeLimitMB"]; + if (long.TryParse(fileSize, out var size) && size > 0) config.FileSizeLimitMB = size; + + // 加载保留日志文件数量 | Load retained file count limit + var retainCount = ConfigurationManager.AppSettings[KeyPrefix + "RetainedFileCountLimit"]; + if (int.TryParse(retainCount, out var count) && count > 0) config.RetainedFileCountLimit = count; + + return config; + } + } +} diff --git a/XP.Common/Logging/Configs/SerilogConfig.cs b/XP.Common/Logging/Configs/SerilogConfig.cs new file mode 100644 index 0000000..af8c4e8 --- /dev/null +++ b/XP.Common/Logging/Configs/SerilogConfig.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; + +namespace XP.Common.Logging.Configs +{ + /// + /// Serilog 日志配置实体(从 App.config 读取)| Serilog logging configuration entity (loaded from App.config) + /// + public class SerilogConfig + { + /// + /// 日志输出根路径(默认:AppData/Files/Logs)| Log output root path (default: AppData/Files/Logs) + /// + public string LogPath { get; set; } = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "Files", "Logs"); + + /// + /// 最低日志级别(Debug/Info/Warn/Error/Fatal)| Minimum log level + /// + public string MinimumLevel { get; set; } = "Info"; + + /// + /// 是否输出到控制台(调试环境=true,生产环境=false)| Whether to output to console + /// + public bool EnableConsole { get; set; } = true; + + /// + /// 日志文件分割规则(Day/Month/Hour/Size)| Log file rolling interval + /// + public string RollingInterval { get; set; } = "Day"; + + /// + /// 单个日志文件最大大小(MB,仅 Size 分割时生效)| Single log file max size (MB) + /// + public long FileSizeLimitMB { get; set; } = 100; + + /// + /// 保留日志文件数量(默认30天)| Retained file count limit (default 30) + /// + public int RetainedFileCountLimit { get; set; } = 30; + } +} diff --git a/XP.Common/Logging/SerilogInitializer.cs b/XP.Common/Logging/SerilogInitializer.cs index 9fd141a..dd322d3 100644 --- a/XP.Common/Logging/SerilogInitializer.cs +++ b/XP.Common/Logging/SerilogInitializer.cs @@ -1,7 +1,7 @@ using System; using Serilog; using Serilog.Events; -using XP.Common.Configs; +using XP.Common.Logging.Configs; using XP.Common.Logging.ViewModels; namespace XP.Common.Logging