LogUtil.cs
4.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Reflection;
using System.Drawing;
namespace OnlineStore.Common
{
public class LogUtil
{
private static LogUtil instance = new LogUtil();
public delegate void ShowLog(string msg, Color color);
public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog");
public static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>();
public static System.Windows.Forms.RichTextBox logBox = null;
public static int showCount = 20;
public static bool debug_opened = false;
public static void info(ILog log,string msg )
{
if (log == null)
{
return;
}
log.Info( " - " + msg);
if (logBox == null)
{
return;
}
AddToBox(msg, Color.Black);
//clear();
}
public static void info(ILog log,string msg, Color color)
{
log.Info( " - " + msg);
if (logBox == null)
{
return;
}
AddToBox(msg, color);
}
public static void debug(ILog log, string msg, Color color)
{
log.Debug( " - " + msg);
if (debug_opened)
{
if (logBox == null)
{
return;
}
AddToBox(msg, color);
}
}
public static void debug(ILog log, string msg)
{
log.Debug( " - " + msg);
if (debug_opened)
{
if (logBox == null)
{
return;
}
AddToBox(msg, Color.Gray);
}
}
public static void error( string errorMsg,int type)
{
if (lastErrorLogTime.ContainsKey(type))
{
TimeSpan span = DateTime.Now - lastErrorLogTime[type];
if (span.TotalSeconds < 10)
{
return;
}
else
{
lastErrorLogTime.Remove(type);
lastErrorLogTime.Add(type, DateTime.Now);
error( errorMsg);
}
}
else
{
lastErrorLogTime.Add(type, DateTime.Now);
error( errorMsg);
}
}
public static void error(ILog log, string errorMsg)
{
//if (!lasErrorLogList.Contains(errorMsg))
{
log.Error( " - " + errorMsg);
if (logBox == null)
{
return;
}
AddToBox(errorMsg, Color.Red);
}
//lasErrorLogList.Add(errorMsg);
//if (lasErrorLogList.Count > errCount)
//{
// lasErrorLogList.RemoveAt(0);
//}
}
private static void AddToBox(string msg, Color color)
{
try
{
ShowLogPro(msg);
}
catch (Exception ex)
{
LOGGER.Error("出错:" + ex.StackTrace);
}
}
private static int count = 0;
private static void ShowLogPro(string msg )
{
try
{
//clear();
if (count > showCount)
{
count = 0;
logBox.Clear();
}
//logBox.SelectionColor = color;
System.DateTime now = System.DateTime.Now;
logBox.AppendText(now.ToLongTimeString() + " " + msg + Environment.NewLine);
count++;
}
catch (Exception ex)
{
LOGGER.Error("出错:" + ex.ToString());
}
}
public static void debug(string msg)
{
debug(LOGGER, msg);
}
public static void error(string errorMsg)
{
error( LOGGER,errorMsg);
}
public static void ClearLog()
{
if (logBox != null)
{
logBox.Text = "";
count = 0;
}
}
public static void info(string msg)
{
info(LOGGER,msg );
}
}
}