123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MV485.helper
- {
- public class Logger
- {
- private static readonly object _lock = new object();
- private static string _logDirectory = "logs"; // 日志文件存放目录
- private static string _logFileName => Path.Combine(_logDirectory, $"{DateTime.Now:yyyy-MM-dd}.log"); // 按日期存放日志文件
- public enum LogLevel
- {
- Debug,
- Info,
- Warn,
- Error
- }
- static Logger()
- {
- //暂时不使用
- // 确保日志目录存在
- /*if (!Directory.Exists(_logDirectory))
- {
- Directory.CreateDirectory(_logDirectory);
- }*/
- }
- public static void Log(LogLevel level, string message)
- {
- //暂时不使用
- /*string logEntry = FormatLogEntry(level, message);
- lock (_lock)
- {
- // 写入日志文件
- File.AppendAllText(_logFileName, logEntry, Encoding.UTF8);
- }
- // 控制台输出(可选)
- Console.Write(logEntry);*/
- }
- public static void Debug(string message) => Log(LogLevel.Debug, message);
- public static void Info(string message) => Log(LogLevel.Info, message);
- public static void Warn(string message) => Log(LogLevel.Warn, message);
- public static void Error(string message) => Log(LogLevel.Error, message);
- private static string FormatLogEntry(LogLevel level, string message)
- {
- return $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{level}] {message}{Environment.NewLine}";
- }
- //-------------------------------------------
- }
- }
|