SystemLog.cs 3.98 KB
using NLog;
using System.Collections.Concurrent;
using System.Windows.Media;

namespace HHECS.DAQClient.Common
{
    /// <summary>
    /// 日志
    /// </summary>
    public class SystemLog
    {
        private static readonly SystemLog _instance = new();
        private readonly ConcurrentQueue<LogModel> _queue = new();
        private readonly Logger _nlog = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 日志队列包含的元素数
        /// </summary>
        public int Count => _queue.Count;
        /// <summary>
        /// 日志队列是否为空
        /// </summary>
        public bool IsEmpty => _queue.IsEmpty;

        private SystemLog() { }

        /// <summary>
        /// 获取日志对象实例
        /// </summary>
        /// <returns></returns>
        public static SystemLog GetInstance()
        {
            return _instance;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="type">日志类型</param>
        public void Log(string message, LogType type = LogType.Info)
        {
            _queue.Enqueue(new LogModel
            {
                Messages = message,
                LogType = type,
            });
            if (type == LogType.Debug)
            {
                _nlog.Error(message);
            }
            else
            {
                _nlog.Info(message);
            }
        }

        /// <summary>
        /// 记录成功日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public void LogSuccess(string message)
        {
            Log(message, LogType.Success);
        }

        /// <summary>
        /// 记录消息日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public void LogInfo(string message)
        {
            Log(message, LogType.Info);
        }

        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public void LogWarning(string message)
        {
            Log(message, LogType.Warning);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public void LogError(string message)
        {
            Log(message, LogType.Error);
        }

        /// <summary>
        /// 输出错误日志信息,并将日志保存至程序错误日志文件
        /// </summary>
        /// <param name="message">日志内容</param>
        public void LogException(string message)
        {
            Log(message, LogType.Debug);
        }

        /// <summary>
        /// 读取一条日志
        /// </summary>
        /// <returns></returns>
        public LogModel GetLog()
        {
            _queue.TryDequeue(out LogModel log);
            return log;
        }
    }

    /// <summary>
    /// 日志实体类,本地日志文件,不存储到数据库
    /// </summary>
    public class LogModel
    {
        public DateTime CreateTime { get; set; } = DateTime.Now;
        public string Messages { get; set; } = string.Empty;

        public LogType LogType { get; set; }

        public SolidColorBrush ForeColor => LogType switch
        {
            LogType.Default => Brushes.Black,
            LogType.Success => Brushes.Green,
            LogType.Info => Brushes.Black,
            LogType.Warning => Brushes.Orange,
            LogType.Error => Brushes.OrangeRed,
            LogType.Debug => Brushes.Red,
            _ => Brushes.Black,
        };
    }

    /// <summary>
    /// 日志级别
    /// </summary>
    public enum LogType
    {
        /// <summary>
        /// 默认
        /// </summary>
        Default,
        Success,
        Info,
        Warning,
        Error,

        /// <summary>
        /// 程序异常日志,Exception 错误信息请使用此类型
        /// </summary>
        Debug
    }
}