feat: add 客户端
This commit is contained in:
50
PetWashControl/Services/LogService.cs
Normal file
50
PetWashControl/Services/LogService.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace PetWashControl.Services;
|
||||
|
||||
public class LogService
|
||||
{
|
||||
private readonly string _logFilePath;
|
||||
|
||||
public LogService()
|
||||
{
|
||||
var logDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
|
||||
Directory.CreateDirectory(logDirectory);
|
||||
_logFilePath = Path.Combine(logDirectory, $"log_{DateTime.Now:yyyyMMdd}.txt");
|
||||
}
|
||||
|
||||
public void LogInfo(string message)
|
||||
{
|
||||
Log("INFO", message);
|
||||
}
|
||||
|
||||
public void LogWarning(string message)
|
||||
{
|
||||
Log("WARN", message);
|
||||
}
|
||||
|
||||
public void LogError(string message, Exception? ex = null)
|
||||
{
|
||||
var fullMessage = ex != null ? $"{message}\n{ex}" : message;
|
||||
Log("ERROR", fullMessage);
|
||||
}
|
||||
|
||||
private void Log(string level, string message)
|
||||
{
|
||||
var logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}";
|
||||
|
||||
// 输出到调试窗口
|
||||
Debug.WriteLine(logMessage);
|
||||
|
||||
// 写入文件
|
||||
try
|
||||
{
|
||||
File.AppendAllText(_logFilePath, logMessage + Environment.NewLine);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略日志写入失败
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user