Initial commit: WpywMail 自建邮件系统:.NET 8 原生 SMTP/IMAP 服务端(DKIM 签名、SPF/DKIM/DMARC 入站校验、SQLite 存储、完整账号体系)、Node 服务端、WinUI 3 客户端与 Web 前端
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System.Text;
|
||||
|
||||
namespace WpywMail.Native;
|
||||
|
||||
/// <summary>极简日志:控制台 + 文件(超过阈值自动滚动一次)。</summary>
|
||||
public static class AppLog
|
||||
{
|
||||
private const long MaxBytes = 20 * 1024 * 1024;
|
||||
private static readonly object Gate = new();
|
||||
private static string path = "";
|
||||
private static bool consoleEnabled = true;
|
||||
|
||||
public static void Configure(AppConfig config, bool enableConsole = true)
|
||||
{
|
||||
Directory.CreateDirectory(config.DataDirectory);
|
||||
path = Path.Combine(config.DataDirectory, "service.log");
|
||||
consoleEnabled = enableConsole;
|
||||
Roll();
|
||||
Info($"日志系统已启动(版本 {BuildInfo.Version})。");
|
||||
}
|
||||
|
||||
public static void Info(string message) => Write("信息", message);
|
||||
public static void Warn(string message) => Write("警告", message);
|
||||
public static void Error(string message) => Write("错误", message);
|
||||
|
||||
private static void Write(string level, string message)
|
||||
{
|
||||
var line = $"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss zzz} [{level}] {message}";
|
||||
if (consoleEnabled)
|
||||
{
|
||||
try { Console.WriteLine(line); } catch { }
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(path)) return;
|
||||
try
|
||||
{
|
||||
lock (Gate) File.AppendAllText(path, line + Environment.NewLine, new UTF8Encoding(false));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
private static void Roll()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(path)) return;
|
||||
var info = new FileInfo(path);
|
||||
if (info.Length < MaxBytes) return;
|
||||
var backup = path + ".1";
|
||||
if (File.Exists(backup)) File.Delete(backup);
|
||||
File.Move(path, backup);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user