實現效果:
知識運用:
EventLog類的CreateEventSource方法 //用於建立一個應用程序 使用指定的Sourc作為向本機上的日志中寫入日志項的有效事件源
CreateEventSource(string source,string logName) //亦可以在本機上創建一個新的自定義日志
Log屬性 //獲取或設置在讀取或寫入的日志名稱
public string Log{get;set;}
Source屬性 //獲取或設置在寫入事件日志時要注冊和使用的源名稱
public string Source{get;set;}
實現代碼:
private void Form1_Load(object sender, EventArgs e) { if (System.Diagnostics.EventLog.SourceExists("ErrEventLog")) { System.Diagnostics.EventLog.DeleteEventSource("ErrEventLog"); } System.Diagnostics.EventLog.CreateEventSource("ErrEventLog","Application"); eventLog1.Log = "Application"; eventLog1.Source = "ErrEventLog"; eventLog1.MachineName = "."; } private void btn_find_Click(object sender, EventArgs e) { if (eventLog1.Entries.Count>0) { foreach(System.Diagnostics.EventLogEntry evn in eventLog1.Entries ) { if (evn.EntryType == System.Diagnostics.EventLogEntryType.Error) listBox1.Items.Add(evn.Message); } } else { MessageBox.Show("系統沒有錯誤日志"); } }