LogExecute.cs
5.78 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
using HHECS.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HHECS.Bll
{
/// <summary>
/// 日志帮助组件
/// </summary>
public class LogExecute
{
private static Dictionary<String, Queue<String>> dic = new Dictionary<String, Queue<String>>();
static readonly string LogPath = "D:\\WcsLog";
public const string ExceptionTag = "ExceptionTag";
public static void WriteInfoLog(string Message, bool IsSucc)
{
string s = IsSucc ? "成功" : "失败";
WriteInfoLog(Message + ",操作结果[" + s + "]");
}
public static void WriteDBExceptionLog(Exception ex)
{
WriteExceptionLog("DBExecute", ex);
}
public static void WriteExceptionLog(string tile, Exception ex)
{
try
{
if (ex != null && ex.Message != ExceptionTag)
{
StringBuilder sb = new StringBuilder();
string NowDateTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
sb.AppendLine(string.Format("****************************{0},Exception[{1}]****************************", NowDateTime, tile));
sb.AppendLine(ex.ToString());
addLog("Exception", sb.ToString());
}
}
catch
{
}
}
public static void WriteInfoLog(string Message)
{
addLog("Info", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + Message);
}
public static void WriteLog(string title,string Message)
{
addLog(title, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + Message);
}
public static void WriteLineDataLog(string Message)
{
addLog("Data", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " " + Message);
}
private static void addLog(string title, string Message)
{
if (dic.ContainsKey(title) == false)
{
Queue<String> queue = new Queue<String>();
dic.Add(title, queue);
}
dic[title].Enqueue(Message);
}
static LogExecute()
{
System.Threading.ThreadPool.QueueUserWorkItem((s) =>
{
while (true)
{
try
{
foreach (var item in dic)
{
if (item.Value.Count > 0)
{
StringBuilder sb = new StringBuilder();
while (item.Value.Count > 0)
{
sb.AppendLine(item.Value.Dequeue());
}
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
string fileFullName = System.IO.Path.Combine(LogPath, string.Format("{0}{1}.log", item.Key, DateTime.Now.ToString("yyyyMMdd")));
if (!System.IO.File.Exists(fileFullName))
{
FileStream stream = System.IO.File.Create(fileFullName);
stream.Close();
}
using (StreamWriter writer = System.IO.File.AppendText(fileFullName))
{
writer.WriteLine(sb.ToString());
writer.Close();
}
}
}
System.Threading.Thread.Sleep(5000);
}
catch (Exception ex) { }
}
});
AutoDeleteOldLog();
}
private static void AutoDeleteOldLog()
{
try
{
if (Directory.Exists(LogPath))
{
DirectoryInfo dirinfo = new DirectoryInfo(LogPath);
IEnumerable<FileInfo> list = dirinfo.GetFiles("*.log").Where(s => s.CreationTime < DateTime.Now.AddDays(-30));
foreach (FileInfo item in list)
{
item.Delete();
}
}
}
catch (Exception msg)
{
}
}
//static void WriteLogExecute(string FileName, string Message)
//{
// //如果日志文件目录不存在,则创建
// if (!Directory.Exists(LogPath))
// {
// Directory.CreateDirectory(LogPath);
// }
// string filename = LogPath + "\\" + FileName + "_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
// try
// {
// FileStream fs = new FileStream(filename, FileMode.Append);
// StreamWriter strwriter = new StreamWriter(fs);
// try
// {
// strwriter.WriteLine(Message);
// strwriter.Flush();
// }
// catch
// {
// }
// finally
// {
// strwriter.Close();
// strwriter = null;
// fs.Close();
// fs = null;
// }
// }
// catch
// {
// }
//}
}
}