Blame view

web/WebMvc/Common/Logging.cs 2.71 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
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.Service.Logs;
using System.Collections.Generic;
using System.Threading;


namespace WebMvc.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)
                    {
唐召明 authored
29
                        _log ??= new Logging();
赖素文 authored
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
                    }
                }
                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);
                    }
                }
            });

            //定时器
            ThreadPool.QueueUserWorkItem(o =>
            {
                var jobLogs = 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))
                            {
                                jobLogs.Add(log);
                            }
                        }
                        new LogService().InsSysJobLog(jobLogs);
                        jobLogs.Clear();
                    }
                    else
                    {
                        Thread.Sleep(SleepTime);
                    }
                }
            });
            Log4NetHelper.Instance.Info("Interface log service  started successfully!");
        }
    }
}