统一 Dump、Logging、Database 等功能的配置文件加载和定义类,优化ConfigLoader,职责分散各个模块。
This commit is contained in:
@@ -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