领胜LDS 键盘AOI检测项目
wells.liu
2020-07-07 5918194fccdb2a2303e713b8d2f3335243b9e2ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
 
namespace Bro.Common.Helper
{
    public class LoggerHelper
    {
        public string LogPath { get; set; }
        public string LogPrefix { get; set; }
 
        public LoggerHelper() { }
        public LoggerHelper(string logPath, string logPrefix)
        {
            LogPath = logPath;
            LogPrefix = logPrefix;
        }
 
        readonly ConcurrentQueue<LogMsg> _logQueue = new ConcurrentQueue<LogMsg>();
        Task _logTask = null;
        readonly object _logLock = new object();
 
        public async void LogAsync(LogMsg msg)
        {
            await Task.Run(() =>
            {
                _logQueue.Enqueue(msg);
 
                lock (_logLock)
                {
                    if (_logTask == null)
                    {
                        _logTask = Task.Run(() =>
                          {
                              if (!Directory.Exists(LogPath))
                              {
                                  Directory.CreateDirectory(LogPath);
                              }
 
                              while (true)
                              {
                                  if (_logQueue.Count > 0)
                                  {
                                      string filePath = Path.Combine(LogPath, $"Log_{(string.IsNullOrWhiteSpace(LogPrefix) ? "" : (LogPrefix + "_"))}{DateTime.Now.ToString("yyyyMMdd")}.txt");
 
                                      using (StreamWriter writer = new StreamWriter(filePath, true, System.Text.Encoding.UTF8))
                                      {
                                          while (_logQueue.Count > 0)
                                          {
                                              if (_logQueue.TryDequeue(out LogMsg log))
                                              {
                                                  writer.WriteLine($"{log.LogTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t{log.Prefix}\t{log.Msg}");
                                              }
                                          }
 
                                          writer.Flush();
                                          writer.Close();
                                      }
                                  }
 
                                  Thread.Sleep(1000);
                              }
                          });
                    }
                }
            });
        }
 
        public void LogAsync(DateTime dt, string prefix, string msg)
        {
            LogAsync(new LogMsg(dt, prefix, msg));
        }
    }
 
    public class LogMsg
    {
        public DateTime LogTime { get; set; }
        public string Prefix { get; set; }
        public string Msg { get; set; }
 
        public LogMsg() { }
        public LogMsg(DateTime dt, string prefix, string msg)
        {
            LogTime = dt;
            Prefix = prefix;
            Msg = msg;
        }
    }
}