ITPub博客

首页 > Linux操作系统 > Linux操作系统 > windows 事件监视器信息查询/写入

windows 事件监视器信息查询/写入

原创 Linux操作系统 作者:taogchan 时间:2011-08-09 09:20:21 0 删除 编辑
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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-08-28

  • 博文量
    200
  • 访问量
    1191826