title: Query the Windows event log in a performant way
body: <p>I've built an ASP.NET web app to view data from the Windows Event Log using databound controls.  The data is retrieved via an <code>ObjectDataSource</code> connected to a mediator class that accesses the <code>EventLog</code>.  When I connect a <code>GridView</code> to the <code>ObjectDataSource</code>, it wants to count the rows in the <code>EventLog</code>. I can do this simply and quickly with:</p>
  
  <pre><code>var log = new EventLog {Log = logName};
  return log.Entries.Count;
  </code></pre>
  
  <p>From my unscientific perspective, it appears to be returning in O(1).  However, if I want to count entries that occur before a certain date or that are via a certain  event source, I cannot find a way to count them that is efficient.  I've tried WMI queries such as:</p>
  
  <pre><code>var query = new ObjectQuery("Select * from Win32_NTLogEvent
                               where LogFile='Application'");
  var searcher = new ManagementObjectSearcher(query);
  var result = searcher.Get();
  var foo = result.Count;
  </code></pre>
  
  <p>For an event log with 70k entries, this takes on the order of a minute on my reasonably powered workstation.  It's looking like O(n).  I've also tried filtering <code>log.Entries</code> with Linq and get similar results.</p>
  
  <p>Is there any more performant way to do this?  For the actual data in the grid, I've found looping over the <code>log.Entries</code> and accessing via index a very performant way to get a collection of entries.</p>