-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLogs.cs
More file actions
239 lines (225 loc) · 9.36 KB
/
Logs.cs
File metadata and controls
239 lines (225 loc) · 9.36 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;
using System.IO;
using System.Windows.Forms;
namespace ECInspect
{
/// <summary>
/// 日志类(单例类)
/// </summary>
sealed class Logs
{
/// <summary>
/// 日志文件夹名称
/// </summary>
private readonly string FolderName = string.Format(@"\LOG\{0}\", "COMM_LOG");
/// <summary>
/// 异常日志文件夹名称
/// </summary>
private readonly string ErrorFolderName = string.Format(@"\LOG\{0}\", "ERROR_LOG");
/// <summary>
/// 保存日志的队列
/// </summary>
private ConcurrentQueue<string> Comm_LOGQueue = new ConcurrentQueue<string>();
/// <summary>
/// 保存异常日志的队列
/// </summary>
private ConcurrentQueue<string> ERROR_LOGQueue = new ConcurrentQueue<string>();
/// <summary>
/// 当前准备写入日志的队列的数量
/// </summary>
private int PresentComm_LOGCount = 0;
/// <summary>
/// 当前准备写入异常日志的队列的数量
/// </summary>
private int PresentERROR_LOGCount = 0;
private bool isFirstWriteLog = true;//是否第一次写日志
private static Logs log;
private Logs()
{
Thread runWriteCommLOG = new Thread(ThreadWriteCommLOG);
runWriteCommLOG.IsBackground = true;
runWriteCommLOG.Start();
Thread runWriteERRORLOG = new Thread(ThreadWriteERRORLOG);
runWriteERRORLOG.IsBackground = true;
runWriteERRORLOG.Start();
}
public static Logs LogsT()
{
if (log == null) log = new Logs();
return log;
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="str">日志内容</param>
public void AddCommLOG(string str)
{
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\t\t";//每行开头时间
string str_record = time + str;
Comm_LOGQueue.Enqueue(str_record);
}
private void ThreadWriteCommLOG()
{
while (true)
{
if (!Comm_LOGQueue.IsEmpty)
{
PresentComm_LOGCount = Comm_LOGQueue.Count;
WriteCommLOG();
PresentComm_LOGCount = 0;
}
Thread.Sleep(5000);
}
}
/// <summary>
/// 写日志
/// </summary>
private void WriteCommLOG()
{
try
{
string filename = DateTime.Now.ToString("yyyyMMddHH");//文件名称
string filenameLastHour = DateTime.Now.AddHours(-1).ToString("yyyyMMddHH");//上一个小时的文件名称
string dirName = Application.StartupPath + FolderName;
if (!Directory.Exists(dirName)) Directory.CreateDirectory(dirName);
string _logfile = dirName + filename + ".log";
string _logfileLastHour = dirName + filenameLastHour + ".log";
FileStream FS = new FileStream(_logfile, FileMode.Append, FileAccess.Write, FileShare.Write);
FileStream FSLastHour = new FileStream(_logfileLastHour, FileMode.Append, FileAccess.Write, FileShare.Write);
StringBuilder writestr = new StringBuilder(PresentComm_LOGCount);
StringBuilder writestrLastHour = new StringBuilder(PresentComm_LOGCount);
for (int i = 0; i < PresentComm_LOGCount; i++)
{
string tempstr = string.Empty; ;
if (Comm_LOGQueue.TryDequeue(out tempstr))
{
DateTime time;
if (tempstr.Length >= 19 && DateTime.TryParse(tempstr.Substring(0, 19), out time))
{
string strtime = time.ToString("yyyyMMddHH");
if (isFirstWriteLog)
{
StringBuilder start = new StringBuilder();
for (int j = 0; j < 80; j++)
{
start.Append("*");
}
start.Insert(start.Length / 2, "程序开启");
tempstr = start.AppendLine().Append(tempstr).ToString();
isFirstWriteLog = false;
}
if (strtime == filename) writestr.AppendLine(tempstr);
else if (strtime == filenameLastHour) writestrLastHour.AppendLine(tempstr);
}
}
else break;
}
StreamWriter SW = new StreamWriter(FS, Encoding.Default);
SW.Write(writestr);
SW.Close();
SW.Dispose();
if (writestrLastHour.Length > 0)
{
StreamWriter SWLastHour = new StreamWriter(FSLastHour, Encoding.Default);
SWLastHour.Write(writestrLastHour);
SWLastHour.Close();
SWLastHour.Dispose();
}
else
{
FSLastHour.Dispose();
if (File.ReadAllBytes(_logfileLastHour).Length == 0) File.Delete(_logfileLastHour);
}
}
catch (Exception ex)
{
//MessageBox.Show("CommLog Error:"+ex.Message+"\r\n"+ex.StackTrace);
Console.WriteLine("\r\n!!!Logs Error\r\n" + ex.Message);
}
}
/// <summary>
/// 添加异常日志
/// </summary>
/// <param name="str">异常日志内容</param>
public void AddERRORLOG(string str)
{
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\t\t";//每行开头时间
string str_record = time + str;
Comm_LOGQueue.Enqueue(str_record);
ERROR_LOGQueue.Enqueue(str_record);
}
private void ThreadWriteERRORLOG()
{
while (true)
{
if (!ERROR_LOGQueue.IsEmpty)
{
PresentERROR_LOGCount = ERROR_LOGQueue.Count;
WriteERRORLOG();
PresentERROR_LOGCount = 0;
}
Thread.Sleep(5000);
}
}
/// <summary>
/// 写异常日志
/// </summary>
private void WriteERRORLOG()
{
try
{
string filename = DateTime.Now.ToString("yyyyMMdd");//文件名称
string filenameLastHour = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");//前一天的文件名称
string dirName = Application.StartupPath + ErrorFolderName;
if (!Directory.Exists(dirName)) Directory.CreateDirectory(dirName);
string _logfile = dirName + filename + ".log";
string _logfileLastHour = dirName + filenameLastHour + ".log";
FileStream FS = new FileStream(_logfile, FileMode.Append, FileAccess.Write, FileShare.Write);
FileStream FSLastHour = new FileStream(_logfileLastHour, FileMode.Append, FileAccess.Write, FileShare.Write);
StringBuilder writestr = new StringBuilder(PresentERROR_LOGCount);
StringBuilder writestrLastHour = new StringBuilder(PresentComm_LOGCount);
for (int i = 0; i < PresentERROR_LOGCount; i++)
{
string tempstr = string.Empty;
if (ERROR_LOGQueue.TryDequeue(out tempstr))
{
DateTime time;
if (tempstr.Length >= 10 && DateTime.TryParse(tempstr.Substring(0, 10), out time))
{
string strtime = time.ToString("yyyyMMdd");
if (strtime == filename) writestr.AppendLine(tempstr);
else if (strtime == filenameLastHour) writestrLastHour.AppendLine(tempstr);
}
}
else break;
}
StreamWriter SW = new StreamWriter(FS, Encoding.Default);
SW.Write(writestr);
SW.Close();
SW.Dispose();
if (writestrLastHour.Length > 0)
{
StreamWriter SWLastHour = new StreamWriter(FSLastHour, Encoding.Default);
SWLastHour.Write(writestrLastHour);
SWLastHour.Close();
SWLastHour.Dispose();
}
else
{
FSLastHour.Dispose();
if (File.ReadAllBytes(_logfileLastHour).Length == 0) File.Delete(_logfileLastHour);
}
}
catch (Exception ex)
{
//MessageBox.Show("Error Error:" + ex.Message + "\r\n" + ex.StackTrace);
Console.WriteLine("\r\n!!!Logs ERROR_LOG Error\r\n" + ex.Message);
}
}
}
}