Using LINQ to XML to generate a RSS feed from the event log by ThinqLinq

Using LINQ to XML to generate a RSS feed from the event log

The Jacksonville Code Camp tried a new concept which was very well received. In it they asked people to bring "cool code" and show it in 7 minutes. Personally, I'm not sure what can be cooler than VB 9's XML literals, so I decided to give it a shot. In the end, I came up with a ONE LINE solution which can iterate over the event log and generate an XML string in the format of a RSS feed which is served up to the Response stream. When I showed the one line, some were skeptical. Granted, there are a couple of line continuation characters, but it is indeed a single function call (which happens to take up 18 lines when formatted for readability...) Anyway, here's the one line:

Private Sub WriteRss()
If EventLog.Exists(LogName) Then _
Response.Write(<channel>
<title>Event Log from <%= Environment.MachineName %></title>
<description>Events logged in the system's <%= LogName %> Event Log</description>
<copyright>This RSS feed is copyright (c) <%= Now.Year.ToString %>.</copyright>
<publisher>Your Name</publisher>
<author>you Lastname</author>
<language>en-US</language>
<%= From entry In New EventLog(LogName).Entries.Cast(Of EventLogEntry)() _
Where (FilterType Is Nothing OrElse FilterType.ToUpper = entry.EntryType.ToString.ToUpper) And _
(EventSourceName Is Nothing OrElse EventSourceName.ToUpper = entry.Source.ToUpper) _
Select <item>
<title><%= entry.EntryType.ToString() & ": " & entry.Source %></title>
<guid isPermaLink="false"><%= Environment.MachineName & "/" & LogName & "/" & entry.Index %></guid>
<description><%= GetItemText(entry) %></description>
<pubDate><%= entry.TimeWritten.ToUniversalTime().ToString() %></pubDate>
<date><%= entry.TimeWritten.ToString() %></date>
<creator><%= entry.UserName %></creator>
</item> %>
</channel>)

End Sub

I acknowledged to Vinay, that I would accept that this could be considered as 18 lines, but still think it is way cool code. Evidently, I wasn't alone as the judges awarded it 3rd place and it won the audience's choice as coolest code. If you want the complete code (less than 100 lines of code including comments) it is available for download as well.

My original intention was to extract and serve emails, but writing the POP3 or MAPI wrapper would have pushed the solution out more than I wanted for the purpose of this contest. It would be easy to convert this code to serve those purposes as well.

Posted on - Comment
Categories: Linq to XML - Code Camp -
comments powered by Disqus