Blame view

HHECS.DAQHandle/Common/Utils/SystemLog.cs 1.64 KB
唐召明 authored
1
2
3
using NLog;

namespace HHECS.DAQHandle.Common.Utils
唐召明 authored
4
{
唐召明 authored
5
    internal static class SystemLog
唐召明 authored
6
    {
唐召明 authored
7
        private static readonly object lockObj = new object();
唐召明 authored
8
9
        private static readonly Logger _nlog = LogManager.GetCurrentClassLogger();
唐召明 authored
10
11
        private static void Print(string message, LogType type = LogType.Info)
        {
12
13
14
            lock (lockObj)
            {
                Console.Write('[');
唐召明 authored
15
                Console.ForegroundColor = type switch
16
                {
唐召明 authored
17
18
19
20
21
22
                    LogType.Success => ConsoleColor.DarkGreen,
                    LogType.Info => ConsoleColor.DarkBlue,
                    LogType.Warn => ConsoleColor.DarkYellow,
                    LogType.Error => ConsoleColor.DarkRed,
                    _ => default
                };
23
                Console.Write($"{type}");
24
                Console.ForegroundColor = ConsoleColor.Gray;
25
26
                Console.Write(']');
                Console.WriteLine($" {DateTime.Now} {message}");
唐召明 authored
27
28
29
30
31
32
33
34
35

                if (type == LogType.Error)
                {
                    _nlog.Error(message);
                }
                else
                {
                    _nlog.Info(message);
                }
36
            }
唐召明 authored
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        }

        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 enum LogType
    {
57
        Success,
唐召明 authored
58
59
60
61
62
        Info,
        Warn,
        Error
    }
}