LINQed IN

Blog and resources on C# and .NET related software development stuff by Troy Magennis 
Welcome to LINQed IN Sign in | Join | Help
in
Home Blogs Files

LINQed IN - Troy Magennis' View on .NET, C# and Software Development

Building a Event Log Viewer using LINQ

Now that LINQ is available to all developers with the release of Visual Studio 2008, it is going to take some time to realize all of its uses. I now find I examine a .NET class library object and see if any properties implement an IEnumerable collection interface.

Like the EventLog component does. You drag this component onto a form and you immediately can read and write to and from a Windows Event Log. I immediately wanted to see if I could use LINQ to filter and query the entries.

My final aim is to make an RSS publisher for certain event log entries that match a filter. But for this example, I just wanted to display and filter the event log sources into a grid.

Image

The full code can be Downloaded here or a full step-by-step available on the HookedONLINQ Wiki site here.

The two important LINQ query bits look like this:

        private void MainForm_Load(object sender, EventArgs e)
        {
            // retrieve a list of all event log sources, and populate the drop-down combo box
            eventLog.Log = "Application";
 
            var sources = (from EventLogEntry es in eventLog.Entries
                           orderby es.Source
                           select es.Source).Distinct();
 
            comboBoxSource.DataSource = sources.ToList();
        }

        private void comboBoxSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            // retrieve all the event log entries matching the source selection in the combo-box
            var query = from EventLogEntry el in eventLog.Entries
                        where el.Source == comboBoxSource.Text
                        orderby el.TimeGenerated descending
                        select new
                        {
                            Time = el.TimeGenerated,
                            Source = el.Source,
                            Message = el.Message
                        };
 
            dataGridView.DataSource = query.ToList();
 
        }
You can see that is pretty simple queries. To do the same using loops and if statements would have been considerable more work though. Keep an eye out for any type that exposes a list property, and keep LINQ in mind when using that property. If you are seeing lots of complicated for loops and if statements - you should refactor and use a LINQ query instead.
Troy. 
Published Tuesday, November 27, 2007 3:39 PM by Troy.Magennis
Filed Under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

What do you think?

(required) 
(optional)
(required) 

This Blog

Post Calendar

<November 2007>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

News

I'm in Seattle, WA at the moment and will be for the next couple of months. Enjoying my holidays and as you can see i'm getting to spends some time on my laptop writing a few blog posts!

Syndication

Powered by Community Server, by Telligent Systems