统一 Dump、Logging、Database 等功能的配置文件加载和定义类,优化ConfigLoader,职责分散各个模块。
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace XP.Common.Database.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// SQLite 配置加载器,从 App.config 读取数据库相关配置项 | SQLite configuration loader, reads database-related configuration from App.config
|
||||
/// </summary>
|
||||
public static class ConfigLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置键前缀 | Configuration key prefix
|
||||
/// </summary>
|
||||
private const string KeyPrefix = "Sqlite:";
|
||||
|
||||
/// <summary>
|
||||
/// 从 App.config 加载 SQLite 配置 | Load SQLite configuration from App.config
|
||||
/// </summary>
|
||||
/// <returns>SQLite 配置实体,缺失或无效配置项使用默认值 | SQLite configuration entity, uses default values for missing or invalid items</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace XP.Common.Database.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// SQLite 配置实体(从 App.config 读取)| SQLite configuration entity (loaded from App.config)
|
||||
/// </summary>
|
||||
public class SqliteConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库文件路径 | Database file path
|
||||
/// </summary>
|
||||
public string DbFilePath { get; set; } = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"Files", "Data", "XP.db");
|
||||
|
||||
/// <summary>
|
||||
/// 连接超时时间(秒,默认30)| Connection timeout (seconds, default 30)
|
||||
/// </summary>
|
||||
public int ConnectionTimeout { get; set; } = 30;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库不存在时是否自动创建(默认true)| Whether to auto-create if not exists (default true)
|
||||
/// </summary>
|
||||
public bool CreateIfNotExists { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用 WAL 模式(提升并发性能,默认true)| Whether to enable WAL mode (default true)
|
||||
/// </summary>
|
||||
public bool EnableWalMode { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否开启日志记录(记录所有SQL操作,默认false)| Whether to enable SQL logging (default false)
|
||||
/// </summary>
|
||||
public bool EnableSqlLogging { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 SQLite 连接字符串 | Get SQLite connection string
|
||||
/// </summary>
|
||||
public string GetConnectionString()
|
||||
{
|
||||
var builder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = DbFilePath,
|
||||
Cache = Microsoft.Data.Sqlite.SqliteCacheMode.Default,
|
||||
DefaultTimeout = ConnectionTimeout
|
||||
};
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace XP.Common.Dump.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// Dump 配置加载器,从 App.config 读取 Dump 相关配置项 | Dump configuration loader, reads dump-related configuration from App.config
|
||||
/// </summary>
|
||||
public static class ConfigLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置键前缀 | Configuration key prefix
|
||||
/// </summary>
|
||||
private const string KeyPrefix = "Dump:";
|
||||
|
||||
/// <summary>
|
||||
/// 从 App.config 加载 Dump 配置 | Load Dump configuration from App.config
|
||||
/// </summary>
|
||||
/// <returns>Dump 配置实体,缺失或无效配置项使用默认值 | Dump configuration entity, uses default values for missing or invalid items</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace XP.Common.Logging.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// Serilog 配置加载器,从 App.config 读取日志相关配置项 | Serilog configuration loader, reads logging-related configuration from App.config
|
||||
/// </summary>
|
||||
public static class ConfigLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置键前缀 | Configuration key prefix
|
||||
/// </summary>
|
||||
private const string KeyPrefix = "Serilog:";
|
||||
|
||||
/// <summary>
|
||||
/// 从 App.config 加载 Serilog 配置 | Load Serilog configuration from App.config
|
||||
/// </summary>
|
||||
/// <returns>Serilog 配置实体,缺失或无效配置项使用默认值 | Serilog configuration entity, uses default values for missing or invalid items</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace XP.Common.Logging.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// Serilog 日志配置实体(从 App.config 读取)| Serilog logging configuration entity (loaded from App.config)
|
||||
/// </summary>
|
||||
public class SerilogConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志输出根路径(默认:AppData/Files/Logs)| Log output root path (default: AppData/Files/Logs)
|
||||
/// </summary>
|
||||
public string LogPath { get; set; } = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"Files", "Logs");
|
||||
|
||||
/// <summary>
|
||||
/// 最低日志级别(Debug/Info/Warn/Error/Fatal)| Minimum log level
|
||||
/// </summary>
|
||||
public string MinimumLevel { get; set; } = "Info";
|
||||
|
||||
/// <summary>
|
||||
/// 是否输出到控制台(调试环境=true,生产环境=false)| Whether to output to console
|
||||
/// </summary>
|
||||
public bool EnableConsole { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 日志文件分割规则(Day/Month/Hour/Size)| Log file rolling interval
|
||||
/// </summary>
|
||||
public string RollingInterval { get; set; } = "Day";
|
||||
|
||||
/// <summary>
|
||||
/// 单个日志文件最大大小(MB,仅 Size 分割时生效)| Single log file max size (MB)
|
||||
/// </summary>
|
||||
public long FileSizeLimitMB { get; set; } = 100;
|
||||
|
||||
/// <summary>
|
||||
/// 保留日志文件数量(默认30天)| Retained file count limit (default 30)
|
||||
/// </summary>
|
||||
public int RetainedFileCountLimit { get; set; } = 30;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user