2010-03-17 4 views
10
  1. 특정 이벤트 (주어진 이벤트 ID, 시간 및 입력 노드)가 기록되는지 여부를 아는 방법?
  2. 예에 대한

, 내가 원하는 등등 로그인 이름, 내가 이벤트 설명과 같은 세부 사항을 얻는 방법 이벤트가 기록되어있는 경우 C# : 주어진 이벤트 ID로 이벤트 로그 세부 정보를 쿼리하는 방법?

  • , [이 경우, 나는 단 하나의 이벤트가 기록됩니다 알] 응용 프로그램 및 서비스 로그> Microsoft> Windows> groupPolicy> Operational 노드에서 이벤트를 쿼리하고 이벤트 ID는 5315이며 시간은 현재 시간입니다.

  • 답변

    17

    새 스타일 Windows EventLog에서 이벤트를 쿼리하는 경우 몇 가지 새로운 트위스트가 있습니다.

    1. 새 이벤트를 읽으려면 System.Diagnostics.Eventing.Reader 네임 스페이스의 클래스를 사용해야합니다.
    2. 쿼리가 Xpath 형식이므로 타이머가 까다로 우며 EventLogQuery definition의 msdn을 참조하십시오.
    3. 프로그램에서 액세스 문제가 발생하여 로깅 컴퓨터의 EventReaders AD 그룹에 포함 된 사용자로 가장 할 수 있습니다.

    이 샘플은 몇 가지 새로운 액세스 방법 인 건배를 보여줍니다.

     string eventID = "5312"; 
         string LogSource = "Microsoft-Windows-GroupPolicy/Operational"; 
         string sQuery = "*[System/EventID=" + eventID + "]"; 
    
         var elQuery = new EventLogQuery(LogSource, PathType.LogName, sQuery); 
         var elReader = new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery); 
    
         List<EventRecord> eventList = new List<EventRecord>(); 
         for (EventRecord eventInstance = elReader.ReadEvent(); 
          null != eventInstance; eventInstance = elReader.ReadEvent()) 
         { 
          //Access event properties here: 
          //eventInstance.LogName; 
          //eventInstance.ProviderName; 
          eventList.Add(eventInstance); 
         } 
    
    +1

    감사 Kellie는 .. 그것은 어떻게 주어진 날짜 withing에 이벤트를 얻기에 관하여 – satya

    +0

    도움이? Microsoft Office 15 Alerts \ ") 및"+ // "(EventID = 300)"+ "(TimeCreated/@ SystemTime > = \ "+ t1 +"\ ")"+ "(TimeCreated/@ SystemTime < = \" "+ t2 +"\ ")"+ " "]] "; 쿼리 예외가 발생합니다. –

    9

    당신은 문제의 이벤트 로그를 조회 할 수 다음 Entries 컬렉션 매우 큰 경향이 있기 때문에

    var sourceName = "MySource"; 
    var el = new EventLog("Application"); 
    var latestEntryTime = (from entry in el.Entries.Cast<EventLogEntry>() 
             where entry.Source == sourceName 
             && // put other where clauses here... 
             orderby entry.TimeWritten descending 
             select entry).First(); 
    

    그러나,이 방법은 느린 있음을 경고.

    관련 문제