服务器日志查询代码

参考网址:http://www.oschina.net/code/explore/mono-2.8.1/mcs/class/System/System.Diagnostics/EventLog.cs

1、服务器日志:应用程序日志、系统日志、安全日志。如下图

2、查询日志代码:EventLogSearch.cs

1 public class EventLogClass:IDisposable
2 {
3 public string EntryType
4 {
5 get;
6 set;
7 }
8 public string GeneratedDate
9 {
10 get;
11 set;
12 }
13 public string GeneratedTime
14 {
15 get;
16 set;
17 }
18 public string Source
19 {
20 get;
21 set;
22 }
23 public string Category
24 {
25 get;
26 set;
27 }
28 public string InstanceId
29 {
30 get;
31 set;
32 }
33 public string UserName
34 {
35 get;
36 set;
37 }
38 public string MachineName
39 {
40 get;
41 set;
42 }
43
44 public void Dispose()
45 {
46 }
47 }
48
49
50 public class EventLogSearch:IDisposable
51 {
52 public static readonly string[] LogTypes = new[] { "Application", "Security", "System", "Internet Explorer" };
53
54 public List<EventLogClass> GetData(string logType, DateTime time, bool IsAfter, EventLogEntryType[] eventEntryTypeEFilters)
55 {
56 if (!LogTypes.Contains(logType))
57 {
58 throw new Exception("参数logType无效!");
59 }
60
61 ArrayList entries = null;
62 List<EventLogClass> list = null;
63 object[] obj = null;
64
65 try
66 {
67 if (IsAfter)
68 {
69 entries = this.FindAferTime(logType, time, eventEntryTypeEFilters);
70 }
71 else
72 {
73 entries = this.FindBeforeTime(logType, time, eventEntryTypeEFilters);
74 }
75
76 if (entries != null && entries.Count > 0)
77 {
78 list = new List<EventLogClass>();
79 foreach (EventLogEntry i in entries)
80 {
81 list.Add(
82 new EventLogClass {
83 EntryType = i.EntryType.ToString(),
84 GeneratedDate = i.TimeGenerated.ToString("yyyy/MM/dd"),
85 GeneratedTime = i.TimeGenerated.ToString("HH/mm/ss"),
86 Source = i.Source.ToString(),
87 Category = i.Category.ToString(),
88 InstanceId = i.InstanceId.ToString(),
89 UserName = i.UserName,
90 MachineName = i.MachineName
91 }
92 );
93 }
94 }
95 }
96 catch (Exception ex)
97 {
98 throw ex;
99 }
100 finally
101 {
102 entries = null;
103 obj = null;
104 }
105
106 return list;
107 }
108
109 public ArrayList FindBeforeTime(string logType, DateTime time, EventLogEntryType[] eventEntryTypeEFilters)
110 {
111 if (!LogTypes.Contains(logType))
112 {
113 throw new Exception("参数logType无效!");
114 }
115
116 ArrayList entries = null;
117 ArrayList entriesArray = null;
118 EventLog log = null;
119 EventLogEntryCollection logEntries = null;
120
121 try
122 {
123 log = new EventLog(logType);
124 logEntries = log.Entries;
125
126 if (logEntries != null && logEntries.Count > 0)
127 {
128 entries = new ArrayList();
129
130 foreach (EventLogEntry logEntry in logEntries)
131 {
132 if (logEntry.TimeGenerated <= time)
133 {
134 if (eventEntryTypeEFilters != null && eventEntryTypeEFilters.Length > 0)
135 {
136 if (eventEntryTypeEFilters.Contains(logEntry.EntryType))
137 {
138 entries.Add(logEntry);
139 }
140 }
141 else
142 {
143 entries.Add(logEntry);
144 }
145 }
146 }
147 }
148
149 entriesArray = entries.Clone() as ArrayList;
150 }
151 catch (Exception ex)
152 {
153 throw ex;
154 }
155 finally
156 {
157 log.Dispose();
158 logEntries = null;
159 entries = null;
160 }
161
162 return entriesArray;
163 }
164
165 public ArrayList FindAferTime(string logType, DateTime time, EventLogEntryType[] eventEntryTypeEFilters)
166 {
167 if (!LogTypes.Contains(logType))
168 {
169 throw new Exception("参数logType无效!");
170 }
171
172 ArrayList entries = null;
173 ArrayList entriesArray = null;
174 EventLog log = null;
175 EventLogEntryCollection logEntries = null;
176
177 try
178 {
179 log = new EventLog(logType);
180 logEntries = log.Entries;
181
182 if (logEntries != null && logEntries.Count > 0)
183 {
184 entries = new ArrayList();
185
186 foreach (EventLogEntry logEntry in logEntries)
187 {
188 if (logEntry.TimeGenerated >= time)
189 {
190 if (eventEntryTypeEFilters != null && eventEntryTypeEFilters.Length > 0)
191 {
192 if (eventEntryTypeEFilters.Contains(logEntry.EntryType))
193 {
194 entries.Add(logEntry);
195 }
196 }
197 else
198 {
199 entries.Add(logEntry);
200 }
201 }
202 }
203 }
204
205 entriesArray = entries.Clone() as ArrayList;
206 }
207 catch (Exception ex)
208 {
209 throw ex;
210 }
211 finally
212 {
213 log.Dispose();
214 logEntries = null;
215 entries = null;
216 }
217
218 return entriesArray;
219 }
220
221
222 public void Dispose()
223 {
224
225 }
226 }

3、页面调用代码:Test01.aspx.cs

View Code
1 public partial class test01 : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5
6 }
7
8 protected void btnAction_Click(object sender, EventArgs e)
9 {
10 List<object[]> data = null;
11 EventLogSearch eventSearch = null;
12 EventLogEntryType[] eventEntryTypeEFilters = null;
13
14 try
15 {
16 eventEntryTypeEFilters = new EventLogEntryType[] {EventLogEntryType.Error};
17 eventSearch = new EventLogSearch();
18
19
20 gvShow.AllowPaging = true;
21 gvShow.PageSize = 10;
22 gvShow.DataSource = eventSearch.GetData(ddlType.SelectedValue, DateTime.Now.AddMonths(-2), true, eventEntryTypeEFilters);
23 gvShow.DataBind();
24 }
25 catch (Exception ex)
26 {
27 throw ex;
28 }
29 finally
30 {
31 data = null;
32 eventSearch = null;
33 }
34 }
35 }

4、效果图:

posted @ 2011-05-22 20:03  tiandong  阅读(1161)  评论(0编辑  收藏  举报