Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Querying the Windows' events with LINQ and Visual Basic

Some time ago, in the Italian MSDN forum about Visual Basic, a user asked how it is possible querying the Windows' log events registry based on the given ID. This is really easy with LINQ.

First, you get a reference to the Windows' events registry via an instance of the System.Diagnostics.EventLog class. The constructor requires as an argument the log name you want to query (e.g. Application, Security, Setup etc.). Once you get the instance, you can write a LINQ query for filtering only entries marked with the given ID. Each entry is represented by EventLogEntry objects. The following code demonstrates how to accomplish this:

        Dim eLog As New EventLog("Application")


        Dim specificEntries = From logEntry In eLog.Entries.Cast(Of EventLogEntry)()

                              Where logEntry.InstanceId = 9010

                              Select logEntry

Notice how the code requires the Cast(Of T) operator which is often necessary in LINQ to Objects scenarios, especially in case the compiler cannot infer the appropriate type. The EventLogEntry.InstanceId property allows comparing the desired ID event (on my machine the ID 9010 is about events generated by Team Foundation Server 2010 Basic). Then it is possible to perform further operations against the events set (e.g. via a For..Each loop) in order to obtain information about each event, such as the application that raised the event (property EventLogEntry.Source) or the event message (property EventLogEntry.Message).


Print | posted on lunedì 8 marzo 2010 01:05 | Filed Under [ Visual Basic LINQ/EF/OData ]


No comments posted yet.

Post Comment

Please add 3 and 8 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET