ASP.NET event log RSS Generator

This is an example RSS feed built on the RSS generator class that will show the entries for an event log from your PC. To use, add a generic handler to your web project and change the code so it inherits from EventLogRSS, as in this example.

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using RSS;

public class Handler2 : EventLogRSS
{
  public Handler2()
  {
    Log = "Application";
  }
}

The EventLogRSS class looks like this

using System;
using System.ComponentModel;
using System.Diagnostics;

namespace RSS
{
  /// <summary>
  /// Generates an RSS feed for entries in the event log.
  /// </summary>
  public class EventLogRSS : RSSGenerator
  {
    /// <summary>
    /// Creates a new <see cref="EventLogRSS"/> instance.
    /// </summary>
    public EventLogRSS()
    {
      Title = "Event Log Feed";
      Description = "Feed for the computer's event log";
    }

    /// <summary>
    /// Populates the event log RSS feed.
    /// </summary>
    protected override void PopulateFeed()
    {
      if (log == null)
        throw new Exception("Log property must be set before populating the feed.");

      ClearItems();
      EventLog eventLog = new EventLog();
      eventLog.Log = log;
      for (int i = eventLog.Entries.Count - 1; i > eventLog.Entries.Count - 11; i--)
      {
        EventLogEntry entry = eventLog.Entries[i];
        if ((sourceFilter != null) && (sourceFilter.Length > 0))
        {
          if (entry.Source == sourceFilter)
            AddItem(entry);
        }
        else
          AddItem(entry);
      }
    }

    private void AddItem(EventLogEntry entry)
    {
      RSSItem item = AddItem();
      item.Description = entry.Message;
      item.Title = entry.EntryType.ToString() + " - " + entry.Message;
      if (item.Title.Length > 100)
        item.Title = item.Title.Substring(0, 100) + "...";
      item.Date = entry.TimeGenerated;
      item.Author = entry.Source;
    }

    #region properties

    private string sourceFilter;
    /// <summary>
    /// Gets or sets the source which will be used to filter on.
    /// </summary>
    [Description("The event source which will be used to filter on")]
    [Category(RSSCategory)]
    public string SourceFilter
    {
      get { return sourceFilter; }
      set { sourceFilter = value; }
    }

    private string log;
    /// <summary>
    /// Gets or sets the log to read (Application, System etc).
    /// </summary>
    [Description("The log to read (Application, System etc)")]
    [Category(RSSCategory)]
    public string Log
    {
      get { return log; }
      set { log = value; }
    }

    #endregion
  }
}