Event log data source






4.58/5 (9 votes)
This is a data source control that lists the events from the Windows event logs.
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 SqlDataSourceControl
and the AccessDataSourceControl
.
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:
- create a "bin" folder inside the application root of your website (if it's not already there)
- copy the assembly file eventlogdatasource.dll into the bin folder
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 two ways to add the control to your page:
- If the control was installed on the toolbox, add a data control to the page first, such as a
GridView
, aDataList
, or aRepeater
. 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. - 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 to your page:
<rw:EventlogDataSource id="EventlogDataSource1" runat="server" >
</rw:EventlogDataSource>
Then, add the data control to the page and connect it to 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 the 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 username 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
- Data source control
Future
Here are some ideas for improvement:
- Designer support
- Support for remote events (on other computers)
- Support for event filtering
If anyone decides to extend this control, or has any comments, bug reports, or questions, then it would be great to hear from you.