Title:       Event log datasource control
Author:      Jos Branders 
Email:       info@rekenwonder.com
Environment: ASP.NET, VB.NET
Keywords:    event log, eventlog, datasource control, data source control
Level:       Intermediate
Description: This is a data control that lists the events from the Windows event logs.
Section      ASP.NET
SubSection   Error tracking

Introduction

In ASP.NET 2.0, accessing data in web pages is handled through data source controls. These are non-visual controls derived from DataSourceControl. Common examples are the SqlDataSource and the AccessDataSource.

Some time ago, I wanted to list events from the Windows event log in a web page. I decided to create a custom data source control, so that I could use a GridView control to display the data.

The code is in VB.NET, but it's not difficult to get it running in C# or in another language.

License

The control is free and open source under the LGPL license.

Installation

Installing the control

You can install the control in the standard way:

You may want to add the control to the toolbox of your editor (Visual Studio or C# Builder). This will allow you to add the control to a page by dragging it. Follow the editor's procedure to add a control to the toolbox.

Using the control

Adding the control to your page

There are 2 ways to add the control to your page:

  1. If the control was installed on the toolbox, add a data control to the page first, such as a GridView, a DataList or a Repeater.
    Next, use the Data Source configuration wizard to choose a data source.
    Make sure there's a copy of the evenlogdatasource.dll assembly in the bin folder.
  2. Add the code manually. Add this line to the top of your page:
    <%@ Register TagPrefix="rw" Namespace="rw" Assembly="eventlogdatasourcecontrol" %>
    Then, add a tag like this where you want the schedule to be displayed:
    <rw:EventlogDataSource id="EventlogDataSource1" runat="server" >
    </rw:EventlogDataSource>
    Then, add the data control to the page, and connect it to the EventlogDataSource.

Setting the event log source

The control will show the event log source that is set as a Parameter. The name of the parameter is "EventlogName". To show the Application log, you should set the parameter to "Application", as shown below:
<rw:EventlogDataSource ID="eventlogdatasource1" runat="server" >
   <Parameters>
      <asp:Parameter Name="EventlogName" Type="String" DefaultValue="Application" />
   </Parameters>
</rw:EventlogDataSource>
You can use any of the Parameter types, such as ControlParameter or QueryStringParameter, to connect the parameter value to an environment value. Use the Parameters setting in the Properties Window. If no parameter is given, the System event log will be used as the data source.

Demo page

The control comes with a demo page demo.aspx that will show a list of all messages in a selected event log.

This page uses a GridView control, and a DropDownList control for selecting one of the available event logs. The EventlogName parameter is set through a ControlParameter, which connects the DropDownList with the data source.

Important: you need to run this page with credentials that have administrative privileges on the server. Check the impersonation tag in the accompanying web.config file, and modify the user name and password according to your own settings. Access to the security log may still be denied.

How it works

To build a data source control, I got some ideas from Nikhil Kothari's code at www.nikhilk.net/DataSourceControlsSummary.aspx. I built two main classes EventLogDataSource and EventLogDataSourceView.

The code reading the event logs is in the ExecuteSelect method of the EventLogDataSourceView class. This code generates a DataTable with a list of events from a given event log:

Protected Overrides Function ExecuteSelect(ByVal arguments As System.Web.UI.DataSourceSelectArguments) _
         As System.Collections.IEnumerable
    Dim dt As New DataTable()
    dt.Columns.Add("Index", System.Type.GetType("System.Int32"))
    dt.Columns.Add("EntryType", System.Type.GetType("System.String"))
    dt.Columns.Add("TimeGenerated", System.Type.GetType("System.DateTime"))
    dt.Columns.Add("Source", System.Type.GetType("System.String"))
    dt.Columns.Add("UserName", System.Type.GetType("System.String"))
    dt.Columns.Add("MachineName", System.Type.GetType("System.String"))
    dt.Columns.Add("Message", System.Type.GetType("System.String"))

    Dim exc As Exception = Nothing
    Dim dv As DataView = Nothing
    Try
        Dim objEventLog As EventLog = New EventLog(_owner.GetEventlogName())
        Dim objEntries As EventLogEntryCollection = objEventLog.Entries
        Dim objEntry As EventLogEntry
        For Each objEntry In objEntries
            Dim dr As DataRow = dt.NewRow()
            dr("Index") = objEntry.Index
            dr("EntryType") = objEntry.EntryType.ToString()
            dr("TimeGenerated") = objEntry.TimeGenerated
            dr("Source") = objEntry.Source
            dr("UserName") = objEntry.UserName
            dr("MachineName") = objEntry.MachineName
            dr("Message") = objEntry.Message
            dt.Rows.Add(dr)
        Next
        dv = New DataView(dt)
        dv.Sort = arguments.SortExpression
    Catch ex As Exception
        exc = ex
    End Try
    Dim statusEventArgs As New EventlogDataSourceStatusEventArgs(exc)
    OnSelected(statusEventArgs)
    If (exc IsNot Nothing And Not statusEventArgs.ExceptionHandled) Then
        Throw exc
    End If
    Return dv
End Function

Points of interest

Future

Here are some ideas for improvement:

If anyone decides to extend this control, or has any comments, bug reports or questions, then it would be great to hear from you.