SystemLog.cs 1.76 KB
using NLog;

namespace HHECS.DAQHandle.Common.Utils
{
    internal static class SystemLog
    {
        private static readonly object lockObj = new object();
        private static readonly Logger _nlog = LogManager.GetCurrentClassLogger();

        private static void Print(string message, LogType type = LogType.Info)
        {
            lock (lockObj)
            {
                Console.Write('[');
                Console.ForegroundColor = type switch
                {
                    LogType.Success => ConsoleColor.DarkGreen,
                    LogType.Info => ConsoleColor.DarkBlue,
                    LogType.Warn => ConsoleColor.DarkYellow,
                    LogType.Error => ConsoleColor.DarkRed,
                    _ => default
                };
                Console.Write($"{type}");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write(']');
                Console.WriteLine($" {DateTime.Now} {message}");

                if (type == LogType.Error)
                {
                    _nlog.Error(message);
                }
                else
                {
                    _nlog.Info(message);
                }
            }
        }

        public static void PrintInfo(string message)
        {
            Print(message, LogType.Info);
        }

        public static void PrintWarn(string message)
        {
            Print(message, LogType.Warn);
        }

        public static void PrintError(string message)
        {
            Print(message, LogType.Error);
        }

        public static void PrintSuccess(string message)
        {
            Print(message, LogType.Success);
        }
    }

    public enum LogType
    {
        Success,
        Info,
        Warn,
        Error
    }
}