136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
using YLErp.Enums;
|
||
|
||
namespace YLErp.Modules.CalculationLogModule
|
||
{
|
||
internal class CalculationLogger
|
||
{
|
||
private readonly static object _lock = new object();
|
||
|
||
/// <summary>
|
||
/// 所有日志集合
|
||
/// </summary>
|
||
private static List<CalculationLog> Options { get; set; } = new List<CalculationLog>();
|
||
|
||
/// <summary>
|
||
/// 配置
|
||
/// </summary>
|
||
private static CalculationLoggerConfig _config = new CalculationLoggerConfig();
|
||
|
||
/// <summary>
|
||
/// 配置记录日志的场景
|
||
/// </summary>
|
||
/// <param name="scenarios">日志场景</param>
|
||
public static void Setup(CalculationLoggerConfig config)
|
||
{
|
||
_config.TotalCountMaxLimit = config.TotalCountMaxLimit;
|
||
_config.ScenariosOfCalcLog = config.ScenariosOfCalcLog;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回配置
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static CalculationLoggerConfig GetConfig()
|
||
{
|
||
return _config;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回所有的日志
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static List<CalculationLog> GetAllLogs()
|
||
{
|
||
return Options.ToList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询Log
|
||
/// </summary>
|
||
/// <param name="logNum">log 编号</param>
|
||
/// <returns></returns>
|
||
public static CalculationLog GetLog(string logNum)
|
||
{
|
||
return Options.FirstOrDefault(b=>b.LogNum==logNum);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按场景添加日志
|
||
/// </summary>
|
||
/// <param name="log">日志对象</param>
|
||
/// <param name="scenarioEnum">日志场景</param>
|
||
public static void AddLogWithScenario(CalculationLog log, CalcScenarioEnum scenarioEnum)
|
||
{
|
||
if (_config.ScenariosOfCalcLog.Contains(scenarioEnum))
|
||
{
|
||
log.Scenario = scenarioEnum;
|
||
AddLogWithLimit(log);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清除所有的日志
|
||
/// </summary>
|
||
public static void ClearAll()
|
||
{
|
||
Options.Clear();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 有限制的添加日志
|
||
/// </summary>
|
||
/// <param name="log">日志对象</param>
|
||
private static void AddLogWithLimit(CalculationLog log)
|
||
{
|
||
var count = GetCurrentTotalCount();
|
||
if (count >= _config.TotalCountMaxLimit)
|
||
{
|
||
// 如果总数量大于限制的数量,先删除头部1000条
|
||
// TotalCountMaxLimit可能会有改动,所以需要(count - _config.TotalCountMaxLimit)
|
||
RemoveLogs(0, count - _config.TotalCountMaxLimit + 1000);
|
||
}
|
||
AddLog(log);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加一条log
|
||
/// </summary>
|
||
/// <param name="log"></param>
|
||
private static void AddLog(CalculationLog log)
|
||
{
|
||
log.LogNum=Guid.NewGuid().ToString();
|
||
lock (_lock)
|
||
{
|
||
Options.Add(log);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除1000条logs
|
||
/// </summary>
|
||
private static void RemoveThousandLogs()
|
||
{
|
||
RemoveLogs(0, 1000);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除logs
|
||
/// </summary>
|
||
/// <param name="index">起始位置</param>
|
||
/// <param name="count">删除数量</param>
|
||
private static void RemoveLogs(int index, int count)
|
||
{
|
||
Options.RemoveRange(index, count - 1);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前Log总数量
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private static int GetCurrentTotalCount()
|
||
{
|
||
return Options.Count;
|
||
}
|
||
}
|
||
}
|