日期:2014-05-18 浏览次数:21386 次
System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
            el.Log = "Application"; //or System, Security
            el.Source = "TestApplication";
            el.WriteEntry("this is a test", System.Diagnostics.EventLogEntryType.Error);
------解决方案--------------------
 从日志里读取信息,需要使用的是EventLog的Entries方法,该方法返回的是一个集合类EventLogEntryCollection的实例,在该集合类中储存的是EventLogEntry类的实例,该类中存储了日志里各记录的信息。可以用以下的程序来返回日志信息。
    
 首先,要实例化一个EventLog类
    
 EventLog sample=new EventLog();
    
 和写入信息不同的是,我们在这需要指定从那个日志里读记录
    
 sample.Log="Application";
    
 指定的是应用程序日志,当然,可以选择别的日志,比如系统日志System,还有安全日志Security
    
 声明一个EventLogEntryCollection类并给它赋值
    
 EventLogEntryCollection myCollection=sample.Entries;
    
 这样,应用程序日志里的信息都存放在了myCollection对象中,我们可以来读取其中的信息。
    
 用foreach语句来访问myCollection中的所有EventLogEntry对象
    
    
    
    
    
 foreach(EventLogEntry test in myCollection)
    
  {
    
  Console.WriteLine(test.Message,test.Source,test.EventID);
    
  }