Clocking hours worked using the Event log

If you are a geek you probably don't keep count of the countless hours you spend before your comp. Assuming you use a Windows 2000 (or a newer) OS, you can find the time you logged in & logged out each day (and the time spent in a day) programmatically from the Event log.

Place the code below in the Page_load event of a C# webform after adding the namespace ... using System.Diagnostics;



string log="System";
string machine="type-your-machine-name-here";

if(!EventLog.Exists(log,machine))
{
Response.Write("The log does not exist!");
return;
}

EventLog aLog = new EventLog();
aLog.Log = log;
aLog.MachineName = machine;

Response.Write("There are {0} entr[yies] in the log:" + aLog.Entries.Count);

foreach (EventLogEntry entry in aLog.Entries)
{
//the event code for Login event is 6005
if (entry.EventID == 6005)
{
Response.Write("Login:" + entry.TimeGenerated+ "");
}
if (entry.EventID == 6006)
{
Response.Write("======Logout:" + entry.TimeGenerated + "" );
}
}


The event log can be both a source and a destination for diagnostic information.. All the classes required for logging events to the windows event log are in the System.Diagnostics package. The most important class is the EventLog class

Comments