Display EventLog in time descending order

  • Thread starter Thread starter Jim Adams
  • Start date Start date
J

Jim Adams

Is there a built-in way to sort a EventLog.Entries by the
TimeGenerated field?

I'd like to display them in an ASP.NET datagrid showing most recent
entries first (descending order).

Thanks,

Jim
 
Hello (e-mail address removed),

<untestedCodeFromOutsideOfAnIde>
ArrayList list = new ArrayList(myEventLog.Entries);
list.Sort(new DateComparer());
myGrid.DataSource = list;
myGrid.DataBind();

class EventLogEntryComparer : IComparer
{
public int Compare(object x, object y)
{
EventLogEntry e1 = (EventLogEntry)x;
EventLogEntry e2 = (EventLogEntry)y;

return DateTime.Compare(e1.TimeGenerated, e2.TimeGenerated);

}
}
</untestedCodeFromOutsideOfAnIde>

While the above code may not work exactly, the basic idea should be there.
The key item to take away is the implementation of the IComparer interface.

Hope this helps!
 
Back
Top