windows 事件監視器資訊查詢/寫入

taogchan發表於2011-08-09
1 eventLog1.Log = "Application";  
2  
3 foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)  
4 {  
5   Label.Text+=entry.Message;  
6 }  
查詢:
using System.Diagnostics;
 string ddd;
 EventLog eventLog1 = new EventLog();
eventLog1.Log = "Application";
                foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
                {
                    ddd= entry.Message;
                    int d = entry.EventID;
                    ddd=entry.UserName;
                }
LINQ:
//宣告EventLog物件
02 EventLog tLog = new EventLog();
03 //選取Application類的Log
04 tLog.Log = "Application";
05   
06 //選出Type為Warring與Error,且時間介於選擇區間的Log
07 var AppLogEntries =
08             from System.Diagnostics.EventLogEntry e2 in
09                 tLog.Entries
10             where (e2.EntryType ==
11                System.Diagnostics.EventLogEntryType.Warning || e2.EntryType ==
12                System.Diagnostics.EventLogEntryType.Error) && e2.TimeGenerated >
13                dtpStart.Value && e2.TimeGenerated < dtpEnd.Value
14             //選出想要的欄位
15             select new
16             {
17                 e2.Source,
18                 e2.InstanceId,
19                 e2.Message,
20                 e2.TimeGenerated,
21                 e2.UserName,
22                 e2.EntryType,
23             };
24   
25 //放到DataSource上
26 dataGridView1.DataSource = AppLogEntries.ToList();
 
寫入:
using System.Diagnostics;

public void WriteToWindowsEvent(string aSource,string aMessage){

  if(!EventLog.SourceExists(aSource)){
    EventLog.CreateEventSource(aSource,"Project");
  }
  EventLog Log = new EventLog("Project");
  oLog.Source = aSource;
  oLog.WriteEntry(aMessage ,EventLogEntryType.Error);
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-704460/,如需轉載,請註明出處,否則將追究法律責任。

相關文章