LogView.cs
6.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
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
using Common;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UControl.UC
{
/// <summary>
/// 日志视图
/// </summary>
internal class LogView
{
ImageList imageList1 = new ImageList();
ListView listView1 = new ListView();
public int MaxDisplayMsg = 50;
public bool ShowDebug = false;
public bool ShowInfo = true;
public bool ShowWarn = true;
public bool ShowError = true;
/// <summary>
/// 初始化视图
/// </summary>
/// <param name="logview"></param>
public void Init(ListView logview)
{
imageList1.Images.Add(UIControl.Properties.Resource.info);
imageList1.Images.Add(UIControl.Properties.Resource.warn);
imageList1.Images.Add(UIControl.Properties.Resource.error);
imageList1.Images.Add(UIControl.Properties.Resource.debug);
listView1 = logview;
InitListView(listView1, imageList1);
LogUtil.ShowDebug += LogUtil_ShowDebug;
LogUtil.ShowInfo += LogUtil_ShowInfo;
LogUtil.ShowWarn += LogUtil_ShowWarn;
LogUtil.ShowError += LogUtil_ShowError;
}
/// <summary>
/// 初始化视图
/// </summary>
/// <param name="logview"></param>
/// <param name="logBean"></param>
public void Init(ListView logview,LogBean logBean)
{
imageList1.Images.Add(UIControl.Properties.Resource.info);
imageList1.Images.Add(UIControl.Properties.Resource.warn);
imageList1.Images.Add(UIControl.Properties.Resource.error);
imageList1.Images.Add(UIControl.Properties.Resource.debug);
listView1 = logview;
InitListView(listView1, imageList1);
logBean.ShowDebug += LogUtil_ShowDebug;
logBean.ShowInfo += LogUtil_ShowInfo;
logBean.ShowWarn += LogUtil_ShowWarn;
logBean.ShowError += LogUtil_ShowError;
}
void LogUtil_ShowError(string msg)
{
if (ShowError)
AddErrorLog(msg);
}
void LogUtil_ShowWarn(string msg)
{
if (ShowError)
AddWarnLog(msg);
}
void LogUtil_ShowInfo(string msg)
{
if (ShowInfo)
AddInfoLog(msg);
}
void LogUtil_ShowDebug(string msg)
{
if (ShowDebug)
AddDebugLog(msg);
}
public void ClearLog()
{
listView1.Items.Clear();
}
void InitListView(ListView listView, ImageList imageList)
{
listView.SmallImageList = imageList;
ColumnHeader columnHeader1 = new ColumnHeader() { Name = "DateTime", Text = "日志时间", Width = 150 };
ColumnHeader columnHeader2 = new ColumnHeader() { Name = "InfoString", Text = "日志信息", Width = listView1.Width - 150 };
listView.Columns.AddRange(new ColumnHeader[] { columnHeader1, columnHeader2 });
listView1.SizeChanged += ListView1_SizeChanged;
listView1.DoubleClick += ListView1_DoubleClick;
listView1.Scrollable = true;
//listView1.GridLines = true;
listView.HeaderStyle = ColumnHeaderStyle.None;
listView.View = View.Details;
listView.HideSelection = false;
listView.SmallImageList = imageList;
}
void ListView1_DoubleClick(object sender, EventArgs e)
{
try
{
if (sender != null)
{
ListView lv = (ListView)sender;
MessageBox.Show(lv.SelectedItems[0].SubItems[1].Text, $"日志信息:{lv.SelectedItems[0].SubItems[0].Text}");
}
}
catch { }
}
void ListView1_SizeChanged(object sender, EventArgs e)
{
listView1.Columns[1].Width = listView1.Width - listView1.Columns[0].Width;
}
void Addlog(int imageIndex, string info)
{
Addlog(listView1, imageList1, imageIndex, info);
}
void Addlog(ListView listView, ImageList imageList, int imageIndex, string info)
{
try
{
if (listView.InvokeRequired)
{
listView.Invoke(new Action(() =>
{
if (listView.Items.Count > MaxDisplayMsg)
{
listView.Items.RemoveAt(MaxDisplayMsg);
}
ListViewItem lstItem = new ListViewItem(" " + DateTime.Now.ToString(), imageIndex);
lstItem.SubItems.Add(info);
lstItem.BackColor = GetBackColor(imageIndex);
listView.Items.Insert(0, lstItem);
}));
}
else
{
if (listView.Items.Count > MaxDisplayMsg)
{
listView.Items.RemoveAt(MaxDisplayMsg);
}
ListViewItem lstItem = new ListViewItem(" " + DateTime.Now.ToString(), imageIndex);
lstItem.SubItems.Add(info);
lstItem.BackColor = GetBackColor(imageIndex);
listView.Items.Insert(0, lstItem);
}
}
catch { }
}
Color GetBackColor(int imagIndex)
{
Color color = Color.White;
switch (imagIndex)
{
case 1:
color = Color.Yellow;
break;
case 2:
color = Color.Red;
break;
}
return color;
}
string preInfo = "";
string preWarn = "";
string preError = "";
string preDebug = "";
void AddDebugLog(string txt)
{
if (!preDebug.Equals(txt))
{
preDebug = txt;
Addlog(3, txt);
}
}
void AddInfoLog(string txt)
{
if (!preInfo.Equals(txt))
{
preInfo = txt;
Addlog(0, txt);
}
}
void AddWarnLog(string txt)
{
if (!preWarn.Equals(txt))
{
preWarn = txt;
Addlog(1, txt);
}
}
void AddErrorLog(string txt)
{
if (!preError.Equals(txt))
{
preError = txt;
Addlog(2, txt);
}
}
}
}