A:全局Common的公共定义
This commit is contained in:
1 parent
83b7ca5971
commit
eaaedd48cf
18 files changed
+366
-1
No files matched your search
@@ -0,0 +1,33 @@
|
||||
using Common.Configuration;
|
||||
|
||||
namespace Common;
|
||||
|
||||
public sealed class AppConfig
|
||||
{
|
||||
// 使用 Lazy<T> 实现线程安全的懒加载单例
|
||||
private static readonly Lazy<AppConfig> _instance = new Lazy<AppConfig>(() => new AppConfig());
|
||||
|
||||
// 提供全局访问点
|
||||
public static AppConfig Instance => _instance.Value;
|
||||
|
||||
// 私有构造函数,防止外部实例化
|
||||
private AppConfig()
|
||||
{
|
||||
var model = ConfigReader.LoadConfig();
|
||||
AppName = model.AppName ?? "MyApp";
|
||||
Version = model.Version ?? "1.0.0";
|
||||
MaxConnection = model.MaxConnection > 0 ? model.MaxConnection : 10;
|
||||
|
||||
// 初始化配置(可以从文件或环境变量读取)
|
||||
AppName = "MyApp";
|
||||
Version = "1.0.0";
|
||||
MaxConnection = 10;
|
||||
}
|
||||
|
||||
// 配置项:只读属性
|
||||
public string AppName { get; }
|
||||
public string Version { get; }
|
||||
public int MaxConnection { get; }
|
||||
|
||||
// 可扩展其他读取配置的方法
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Common;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.1-dev-02317" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Logger\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Common.Configuration;
|
||||
|
||||
public static class ConfigLoader
|
||||
{
|
||||
private static readonly object _lock = new();
|
||||
private static AppConfig _config;
|
||||
|
||||
public static AppConfig Config
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_config == null)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_config == null)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
||||
|
||||
IConfiguration configuration = builder.Build();
|
||||
|
||||
// _config = new AppConfig();
|
||||
configuration.GetSection("AppSettings").Bind(_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
return _config;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Common.Configuration;
|
||||
|
||||
public static class ConfigReader
|
||||
{
|
||||
public static AppConfigModel LoadConfig()
|
||||
{
|
||||
var env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true);
|
||||
|
||||
IConfiguration config = builder.Build();
|
||||
|
||||
var model = new AppConfigModel();
|
||||
config.GetSection("AppSettings").Bind(model);
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
// 可单独定义模型类用于绑定
|
||||
public class AppConfigModel
|
||||
{
|
||||
public string AppName { get; set; }
|
||||
public string Version { get; set; }
|
||||
public int MaxConnection { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
namespace Common;
|
||||
|
||||
public class ExceptionExpand : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认异常编码
|
||||
/// </summary>
|
||||
private const long DefaultCode = -1;
|
||||
|
||||
public string ErrorMessage { get; private set; } = "";
|
||||
public long ErrorCode { get; private set; } = DefaultCode;
|
||||
public string Suggestion { get; private set; } = "";
|
||||
|
||||
private ExceptionExpand()
|
||||
{
|
||||
}
|
||||
|
||||
public ExceptionExpand(long errorCode, string massage, string solution) : base(
|
||||
massage)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
Suggestion = solution;
|
||||
}
|
||||
|
||||
public ExceptionExpand(long errorCode, string massage, string solution, Exception? innerException) : base(
|
||||
massage, innerException)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
Suggestion = solution;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Error Code: {ErrorCode}\nMessage: {Message}\nSuggestion: {Suggestion}\n{base.ToString()}";
|
||||
}
|
||||
}
|
||||
|
||||
public class ExceptionEnum
|
||||
{
|
||||
// region 系统常见错误本地化
|
||||
|
||||
/// <summary>
|
||||
/// 进程信息获取失败
|
||||
/// </summary>
|
||||
public static readonly ExceptionExpand ProcessGetException = new(100001L, "进程消息获取失败,指定线程可能不存在或已关闭!", "以管理员权限重试!");
|
||||
|
||||
|
||||
// endregion
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Common.Monitors;
|
||||
|
||||
public class ApplicationHealthMonitor
|
||||
{
|
||||
private readonly Timer _healthCheckTimer;
|
||||
|
||||
public ApplicationHealthMonitor()
|
||||
{
|
||||
_healthCheckTimer = new Timer(CheckApplicationHealth, null,
|
||||
TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5));
|
||||
}
|
||||
|
||||
private void CheckApplicationHealth(object state)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查内存使用
|
||||
var memoryUsage = GC.GetTotalMemory(false);
|
||||
if (memoryUsage > 500 * 1024 * 1024) // 500MB
|
||||
{
|
||||
// Logger.Warn($"内存使用过高: {memoryUsage / 1024 / 1024}MB");
|
||||
GC.Collect(); // 强制垃圾回收
|
||||
}
|
||||
|
||||
// 检查线程数量
|
||||
var threadCount = Process.GetCurrentProcess().Threads.Count;
|
||||
if (threadCount > 100)
|
||||
{
|
||||
// Logger.Warn($"线程数量过多: {threadCount}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// LogException(ex, "健康检查异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
| 类别 | 示例 |
|
||||
| -------- | --------------------------------------------- |
|
||||
| ✅ 配置封装 | `AppConfig`, `ConfigReader` |
|
||||
| ✅ 通用工具类 | 字符串处理、加密、哈希、路径处理 |
|
||||
| ✅ 基础模型类 | `Result<T>`, `PagedList<T>`, `ErrorInfo` |
|
||||
| ✅ 日志适配器 | 封装 `Serilog`、`NLog` 的通用调用接口 |
|
||||
| ✅ 通用扩展方法 | `string.ToSafeInt()`, `DateTime.ToUnixTime()` |
|
||||
| ✅ 接口定义 | 如 `INetworkMonitor`, `ILogger`(便于依赖注入) |
|
||||
| ✅ 网络协议模型 | 公共 API 的数据结构定义(DTO/VO) |
|
||||
| ✅ 常量/枚举 | 错误码、状态码、常量定义 |
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Application.Utils;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class SysHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取可用端口
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int GetAvailablePort()
|
||||
{
|
||||
TcpListener listener = new TcpListener(IPAddress.Loopback, 0); // 端口号为0表示由系统自动分配
|
||||
listener.Start();
|
||||
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
|
||||
listener.Stop();
|
||||
return port;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user