Sample console application that read the content of an event log…
——–
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace EventLogReader{
public class Program {
static void Main (string[] args){
EventLog logger=new EventLog("Application");
int maxLineLength=30;
string logFilter="*"; //*, I, W, E, ?
string message;
string type;
int counter=0;
DateTime start, end;
TimeSpan executionTime;
start=DateTime.Now;
foreach (EventLogEntry entry in logger.Entries){
message=entry.Message.Replace("\n", " ");
if (message.Length>maxLineLength) message=message.Substring(0, maxLineLength)+"…";
switch (entry.EntryType){
case EventLogEntryType.Information: type="I";
break;
case EventLogEntryType.Warning: type="W";
break;
case EventLogEntryType.Error: type="E";
break;
default: type="?";
break;
}
if (logFilter=="*" || logFilter==type) {
Console.WriteLine("[{4}>{0}-{1}({2})] {3}", type, entry.Source, entry.InstanceId, message, entry.TimeWritten.ToString("dd/MM/yyyy HH:mm:ss"));
Console.Out.Flush();
counter++;
}
}
end=DateTime.Now;
executionTime=end-start;
Console.WriteLine("{0} row(s) displayed in {1} ms. Press a key to quit…", counter, executionTime.TotalMilliseconds);
Console.Read();
}
}
}