实现效果:
知识运用:
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("系统没有错误日志"); }
}