forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cs
359 lines (337 loc) · 10.4 KB
/
Log.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Threading;
using CYQ.Data.Tool;
namespace CYQ.Data
{
internal static class Error
{
/// <summary>
/// 抛出异常
/// </summary>
/// <param name="msg"></param>
internal static object Throw(string msg)
{
//#if DEBUG
// return "";
//#else
throw new Exception("V" + AppConfig.Version + " " + msg);
//#endif
}
}
/// <summary>
/// LogType
/// <para>日志类型</para>
/// </summary>
public enum LogType
{
/// <summary>
/// 调试
/// </summary>
Debug,
/// <summary>
/// 信息
/// </summary>
Info,
/// <summary>
/// 警告
/// </summary>
Warn,
/// <summary>
/// 错误
/// </summary>
Error,
/// <summary>
/// 断言
/// </summary>
Assert
}
/// <summary>
/// Write Log to txt or database
/// <para>日志静态类(可将日志输出到文本中)</para>
/// </summary>
public static class Log
{
/// <summary>
/// 内部写日志
/// </summary>
/// <param name="message"></param>
internal static void WriteLog(string message)
{
WriteLog(false, message);
}
internal static void WriteLog(bool isWriteLog, string message)
{
if (isWriteLog || AppConfig.Log.IsWriteLog)
{
if (message.Contains(":OpenCon()"))//数据库链接异常不再写数据库(因为很多情况都指向同一个库)
{
WriteLogToTxt(message, LogType.Error);
}
else
{
WriteLogToDB(message, LogType.Error);
}
}
else
{
Error.Throw("Error : " + message);
}
}
/// <summary>
/// write log to database [LogConn must has config value]
/// <para>将日志写到数据库中[需要配置LogConn项后方生效 ]</para>
/// </summary>
public static void WriteLogToDB(Exception err)
{
WriteLogToDB(GetExceptionMessage(err));
}
/// <summary>
/// write log to database [LogConn must has config value]
/// <para>将日志写到数据库中[需要配置LogConn项后方生效 ]</para>
/// </summary>
public static void WriteLogToDB(string message)
{
WriteLogToDB(message, LogType.Error);
}
public static void WriteLogToDB(string message, LogType logType)
{
WriteLogToDB(message, logType, "System");
}
public static void WriteLogToDB(string message, LogType logType, string userName)
{
WriteLogToDB(message, logType.ToString(), userName);
}
public static void WriteLogToDB(string message, string logType, string userName)
{
string conn = AppConfig.Log.LogConn;
if (string.IsNullOrEmpty(conn))
{
WriteLogToTxt("[LogConnIsEmpty]:" + message);
return;
}
try
{
string pageUrl = string.Empty;
if (AppConfig.IsWeb)
{
System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
pageUrl = request.Url.Scheme + "://" + request.Url.Authority + request.RawUrl;
}
else
{
pageUrl = AppConst.RunFolderPath;
}
using (SysLogs el = new SysLogs())
{
el.AllowWriteLog = false;
el.LogType = logType.ToString();
el.PageUrl = pageUrl;
el.Message = message;
el.UserName = userName;
el.CreateTime = DateTime.Now;
el.Insert(InsertOp.None);
}
}
catch
{
WriteLogToTxt("[WriteDbLogError]:" + message);
}
}
#region 日志写到文件
/// <summary>
/// Write log to txt
/// <para>将日志写到外部txt[web.config中配置路径,配置项为Logpath,默认路径为 "Log/" ]</para>
/// </summary>
public static void WriteLogToTxt(string message)
{
WriteLogToTxt(message, null);
}
public static void WriteLogToTxt(string message, LogType logType)
{
WriteLogToTxt(message, logType.ToString());
}
public static void WriteLogToTxt(string message, string logType)
{
try
{
string folder = AppConfig.WebRootPath;
string logPath = AppConfig.Log.LogPath;
if (logPath.StartsWith("~/"))
{
logPath = logPath.Substring(2);
}
folder = folder + logPath;
if (!System.IO.Directory.Exists(folder))
{
System.IO.Directory.CreateDirectory(folder);
}
string todayKey = DateTime.Today.ToString("yyyyMMdd") + ".txt";
if (!string.IsNullOrEmpty(logType))
{
if (logType.EndsWith(".txt"))
{
todayKey = logType;
}
else
{
todayKey = logType.TrimEnd('_') + '_' + todayKey;
}
}
string pageUrl = Log.Url;
string filePath = folder + todayKey;
pageUrl += "\r\n------------------------\r\n";
pageUrl += "V" + AppConfig.Version + " Record On : " + DateTime.Now.ToString() + " " + pageUrl;
message = pageUrl + "\r\n" + message;
IOHelper.Save(filePath, message, true, false);
}
catch //(Exception err)
{
//Error.Throw("Log.WriteLogToTxt() : " + err.Message);
}
}
public static void WriteLogToTxt(Exception err)
{
if (err is ThreadAbortException)//线程中止异常不记录
{
return;
}
string message = GetExceptionMessage(err);
WriteLogToTxt("[Exception]:" + message + "\r\n" + err.StackTrace);
}
#endregion
internal static string Url
{
get
{
string pageUrl = string.Empty;
if (AppConfig.IsWeb)
{
System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
pageUrl = request.Url.Scheme + "://" + request.Url.Authority + request.RawUrl;
if (request.UrlReferrer != null && request.Url != request.UrlReferrer)
{
pageUrl += "\r\nReferer:" + request.UrlReferrer.ToString();
}
}
return pageUrl;
}
}
/// <summary>
/// Convert Exception to string
/// <para>获取异常的内部信息</para>
/// </summary>
public static string GetExceptionMessage(Exception err)
{
string message = err.Message;
if (err.InnerException != null)
{
message += ":" + err.InnerException.Message + "\r\n" + err.InnerException.StackTrace;
}
else
{
message += ":\r\n" + err.StackTrace;
}
return message;
}
}
/// <summary>
/// A class for you to Write log to database
/// <para>日志记录到数据库(需要配置LogConn链接后方有效)</para>
/// </summary>
public class SysLogs : Orm.SimpleOrmBase
{
public SysLogs()
{
base.SetInit(this, AppConfig.Log.LogTableName, AppConfig.Log.LogConn);
}
private int _ID;
/// <summary>
/// 标识主键
/// </summary>
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}
private string _LogType;
/// <summary>
/// 日志类型
/// </summary>
public string LogType
{
get { return _LogType; }
set { _LogType = value; }
}
private string _PageUrl;
/// <summary>
/// 请求的地址
/// </summary>
public string PageUrl
{
get
{
if (string.IsNullOrEmpty(_PageUrl))
{
_PageUrl = Log.Url;
}
return _PageUrl;
}
set
{
_PageUrl = value;
}
}
private string _Message;
/// <summary>
/// 日志内容
/// </summary>
public string Message
{
get
{
return _Message;
}
set
{
_Message = value;
}
}
private string _UserName;
/// <summary>
/// 记录者用户名
/// </summary>
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private DateTime _CreateTime;
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime
{
get
{
if (_CreateTime == DateTime.MinValue)
{
_CreateTime = DateTime.Now;
}
return _CreateTime;
}
set
{
_CreateTime = value;
}
}
}
}