Blame view

Hh.Mes.Api/Common/Logging.cs 3.04 KB
赖素文 authored
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.Service.Logs;
using System.Collections.Generic;
using System.Threading;

namespace Hh.Mes.API.Common
{
    public class Logging
    {
        private static Logging _log;
        private static readonly object LockHelper = new object();
        private readonly int SleepTime = 10000;
        private readonly int logCount = 1;

        private Logging()
        {
        }

        public static Logging GetInstance
        {
            get
            {
                if (_log == null)
                {
                    lock (LockHelper)
                    {
                        if (_log == null)
                        {
                            _log = new Logging();
                        }
                    }
                }
                return _log;
            }
        }

        /// <summary>
        /// 写数据接口日志 只调用一次
        /// </summary>
        public void InitWriteInterLog()
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                var logs = new List<sys_interface_log>();
                while (true)
                {
                    if (QueueInterLog.GetInstance.Queue().Count >= logCount)
                    {
                        for (var i = 0; i < logCount; i++)
                        {
                            if (QueueInterLog.GetInstance.Queue().TryDequeue(out var log))
                            {
                                logs.Add(log);
                            }
                        }
                        new LogService().InsSysInterLog(logs);
                        logs.Clear();
                    }
                    else
                    {
                        Thread.Sleep(SleepTime);
                    }
                }
            });

            Log4NetHelper.Instance.Info("InitWriteInterLog  service  started successfully!");

            InitWriteJobLog();
        }


        /// <summary>
        /// 写数据接口日志 只调用一次
        /// </summary>
        public void InitWriteJobLog()
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                var logs = new List<sys_job_log>();
                while (true)
                {
                    if (QueueJobLog.GetInstance.Queue().Count >= logCount)
                    {
                        for (var i = 0; i < logCount; i++)
                        {
                            if (QueueJobLog.GetInstance.Queue().TryDequeue(out var log))
                            {
                                logs.Add(log);
                            }
                        }
                        new LogService().InsSysJobLog(logs);
                        logs.Clear();
                    }
                    else
                    {
                        Thread.Sleep(SleepTime);
                    }
                }
            });

            Log4NetHelper.Instance.Info("InitWriteJobLog log service  started successfully!");
        }
    }
}