Merge branch 'master' of https://github.com/Meloong-Git/MeloongCore
This commit is contained in:
commit
48a7450f2f
2 files changed
+14
-15
No files matched your search
@@ -19,7 +19,7 @@ public class JsonConfigProvider : IConfigProvider {
|
|||||||
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 'required' 修饰符或声明为可以为 null。
|
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 'required' 修饰符或声明为可以为 null。
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
allProviders.Add(new(this));
|
allProviders.Add(new(this));
|
||||||
saveAction = Throttler.Throttle(Save, TimeSpan.FromMilliseconds(500), leading: false, trailing: true);
|
saveWorker = new(Save, 500, RateLimitMode.WaitUnillNoNewInvoke);
|
||||||
InitJson();
|
InitJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,11 +102,11 @@ public class JsonConfigProvider : IConfigProvider {
|
|||||||
|
|
||||||
// ===================================== 保存 =====================================
|
// ===================================== 保存 =====================================
|
||||||
|
|
||||||
private readonly RateLimitedAction saveAction;
|
private readonly RateLimitedWorker saveWorker;
|
||||||
private bool isDirty = false;
|
private bool isDirty = false;
|
||||||
public void MakeDirty() {
|
public void MakeDirty() {
|
||||||
isDirty = true;
|
isDirty = true;
|
||||||
saveAction.Invoke();
|
saveWorker.Start();
|
||||||
}
|
}
|
||||||
public void Save() {
|
public void Save() {
|
||||||
if (!isDirty) return;
|
if (!isDirty) return;
|
||||||
|
|||||||
+11
-12
@@ -202,7 +202,7 @@ public class BaseLogger {
|
|||||||
(message is null ? "" : $"{message}:") + ex.GetDisplay(true);
|
(message is null ? "" : $"{message}:") + ex.GetDisplay(true);
|
||||||
formattedMessage = Format(formattedMessage, level, filePath, ex);
|
formattedMessage = Format(formattedMessage, level, filePath, ex);
|
||||||
// 将格式化后的日志文本置入缓冲区
|
// 将格式化后的日志文本置入缓冲区
|
||||||
if (avaliable || queue.Count < 100) { // 在初始化前保留至多 100 条日志
|
if (available || queue.Count < 100) { // 在初始化前保留至多 100 条日志
|
||||||
queue.Enqueue(new(level, formattedMessage));
|
queue.Enqueue(new(level, formattedMessage));
|
||||||
queuedEvent.Set();
|
queuedEvent.Set();
|
||||||
}
|
}
|
||||||
@@ -247,27 +247,23 @@ public class BaseLogger {
|
|||||||
|
|
||||||
#region 缓冲区与初始化
|
#region 缓冲区与初始化
|
||||||
|
|
||||||
internal volatile bool avaliable = false;
|
internal volatile bool available = false;
|
||||||
public record struct LogEntry(LogLevel Level, string message);
|
public record struct LogEntry(LogLevel Level, string message);
|
||||||
internal readonly ConcurrentQueue<LogEntry> queue = new();
|
internal readonly ConcurrentQueue<LogEntry> queue = new();
|
||||||
internal readonly AutoResetEvent queuedEvent = new(false);
|
internal readonly AutoResetEvent queuedEvent = new(false);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化此 Logger。
|
|
||||||
/// 会在新线程中,将之前的日志文件依次后移,最多保留 5 个文件。
|
|
||||||
/// </summary>
|
|
||||||
public BaseLogger() {
|
public BaseLogger() {
|
||||||
var thread = new Thread(() => {
|
var thread = new Thread(() => {
|
||||||
try {
|
try {
|
||||||
Init();
|
Init();
|
||||||
avaliable = true;
|
available = true;
|
||||||
Logger.Info($"{GetType().Name} 日志初始化结束");
|
Logger.Info($"{GetType().Name} 日志初始化结束");
|
||||||
while (avaliable) {
|
while (available) {
|
||||||
queuedEvent.WaitOne();
|
queuedEvent.WaitOne();
|
||||||
Flush();
|
Flush();
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
avaliable = false;
|
available = false;
|
||||||
Logger.SendToDebug(ex, "基础日志线程出错");
|
Logger.SendToDebug(ex, "基础日志线程出错");
|
||||||
}
|
}
|
||||||
}) { Name = nameof(Logger), Priority = ThreadPriority.Lowest, IsBackground = true };
|
}) { Name = nameof(Logger), Priority = ThreadPriority.Lowest, IsBackground = true };
|
||||||
@@ -280,7 +276,7 @@ public class BaseLogger {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Flush() {
|
public void Flush() {
|
||||||
try {
|
try {
|
||||||
if (!avaliable) return;
|
if (!available) return;
|
||||||
lock (flushLock) {
|
lock (flushLock) {
|
||||||
while (queue.TryDequeue(out LogEntry entry)) Output(entry); // writer 指定了 AutoFlush
|
while (queue.TryDequeue(out LogEntry entry)) Output(entry); // writer 指定了 AutoFlush
|
||||||
}
|
}
|
||||||
@@ -305,7 +301,10 @@ public class FileLogger : BaseLogger, IDisposable {
|
|||||||
public string fileNameSuffix = ".txt";
|
public string fileNameSuffix = ".txt";
|
||||||
private StreamWriter? writer;
|
private StreamWriter? writer;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <summary>
|
||||||
|
/// 初始化此 Logger。
|
||||||
|
/// 会在新线程中,将之前的日志文件依次后移,最多保留 5 个文件。
|
||||||
|
/// </summary>
|
||||||
public override void Init() {
|
public override void Init() {
|
||||||
// 轮转日志文件,将 Log1.txt 留空
|
// 轮转日志文件,将 Log1.txt 留空
|
||||||
Logger.Info("开始轮转日志文件");
|
Logger.Info("开始轮转日志文件");
|
||||||
@@ -335,7 +334,7 @@ public class FileLogger : BaseLogger, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
avaliable = false;
|
available = false;
|
||||||
if (writer is not null) ((IDisposable) writer).Dispose();
|
if (writer is not null) ((IDisposable) writer).Dispose();
|
||||||
queuedEvent.Dispose();
|
queuedEvent.Dispose();
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
|
|||||||
Reference in new issue
Block a user