Read event log from end

K

krukinews

Hi,

I'm trying to write small program that reads events from computer
and stores them in SQL database.

But I don't know hove to read logs for let's say last 24 hours,
one solution is to read all events in event log and then check time.

foreach(EventLogEntry Entry in logSystem.Entries) {
//
// check time
//
}

And if a have 100k entries, this can take time.
Can I read logs from last to first?

Mihalic Krunoslav
 
D

Dale Preston

To read from the end, just use a for loop and Entries.Count. Be careful
about reading all the entries in first It can be many megabytes of data. I
recommend reading them in one at a time and stopping when you get past 1 day
old, like this:

EventLog myLog = new EventLog("System");

for(int x = myLog.Entries.Count - 1; x >= 0; x--)

{

EventLogEntry entry = myLog.Entries[x];

if (entry.TimeGenerated < DateTime.Now.AddDays(-1))

{

break;

}


// save the item

this.MySaveToSqlMethod(entry.TimeGenerated, entry.Message);

}



HTH



Dale Preston

MCAD, MCDBA, MCSE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top