eWorld.UI - Matt Hawley

Ramblings of Matt

Reading Eventlog Entries

March 15, 2004 17:12 by matthaw

I was asked by my boss the other week, in my spare time at work, to create a script or windows service that would read all entries in the Application Event Log for that day. He pointed me to a link that used VBScript and some wacky dll named "winmgmnts". This seemed like a pain, and I was like...I know you can write to Event Logs with .NET, why couldn't you read them...sure enough, you can. The following code is used to read all entries for the current day of 3 defined logs. This, obviously, can be expanded further...which is what I intend to do.


using System;
using System.Diagnostics;

public class MyClass
{
  public static void Main()
  {
    int month = DateTime.Today.Month;
    int day = DateTime.Today.Day;
    int year = DateTime.Today.Year;

    string[] logNames = new string[] {"Application", "Security", "System"};

    foreach(string log in logNames)
    {
      EventLog appLog = new EventLog(log);

      foreach(EventLogEntry entry in appLog.Entries)
      {
        if(entry.TimeGenerated.Month == month && entry.TimeGenerated.Day == day && entry.TimeGenerated.Year == year)
        {
          Console.WriteLine("-----------------------------------------");
          Console.WriteLine(entry.EntryType.ToString());
          Console.WriteLine(entry.Message);
        }
      }
      Console.ReadLine();
    }
  }
}

[Previously Posted at old Weblog on October 1, 2003]



Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed
Comments are closed

Copyright © 2000 - 2024 , Excentrics World