alielie
2026-04-23 ef52fd6fd07508883dce4eb002acda59f0169e59
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
106
using HMI.Core.Api;
using HMI.Core.Util;
using log4net;
using System;
using System.IO;
 
namespace WPFProject.Utils
{
    /// <summary>
    /// 往sqlite写日志
    /// </summary>
    class CommonLogger : ILogWriter
    {
        private static ILog log = LogManager.GetLogger("CommonLogger");
        private SqliteHelper sqliteHelper;
        private DateTime currentDate;
 
        public CommonLogger()
        {
            RefreshConnection();
        }
 
        private SqliteHelper InitHelper(DateTime date)
        {
            string file = Path.Combine(PathHelper.LogViewerDir, $"log_{date.ToString("yyyy-MM-dd")}.db");
            if (!File.Exists(file))
            {
                SqliteHelper.CreateDatabase(file);
            }
            var sqliteHelper = new SqliteHelper(file);
            sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS log (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    level TEXT NOT NULL,
                    text TEXT NOT NULL,
                    source TEXT,
                    time TIME
                    );");
            return sqliteHelper;
        }
 
        private void RefreshConnection()
        {
            if (sqliteHelper == null)
            {
                currentDate = DateTime.Today;
                sqliteHelper = InitHelper(currentDate);
                return;
            }
            else
            {
                lock (this)
                {
                    try
                    {
                        var current = DateTime.Today;
                        if (current == currentDate) return;
                        sqliteHelper?.Close();
                        sqliteHelper = null;
                        currentDate = current;
                        sqliteHelper = InitHelper(currentDate);
                    }
                    catch (Exception ex)
                    {
                        log.Error("RefreshConnection Failed.", ex);
                        sqliteHelper = null;
                    }
                }
            }
        }
 
        public void Debug(string source, string content)
        {
            DoLog("Debug", source, content);
        }
 
        public void Info(string source, string content)
        {
            DoLog("Info", source, content);
        }
        public void Warn(string source, string content)
        {
            DoLog("Warn", source, content);
        }
 
        public void Error(string source, string content)
        {
            DoLog("Error", source, content);
        }
 
        private void DoLog(string level, string source, string content)
        {
            if (currentDate != DateTime.Today || sqliteHelper == null)
            {
                RefreshConnection();
            }
            if (sqliteHelper == null)
            {
                log.Warn($"Level:{level} Source:{source} {content}");
            }
            else
            {
                sqliteHelper.ExecuteSql($"INSERT INTO log (level, text, source, time) VALUES ('{level}', '{content}', '{source}', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}');");
            }
        }
    }
}