It’s been a while since I wrote an ASP.NET app, at least 5 years. ? I needed to query remote servers to get the last X numbers of Event log entries. One snag I ran into was trying to use the DataGrid or GridView controls. I kept getting “Index out of bounds” error when trying to write the Message (Description) part of an event log message. I reverted to my ‘old Classic ASP’ days and used response.write. I never did find a solution using the built-in controls. For those who want to allow others to view event logs w/o granting local access, your application pool needs the appropriate permissions. I setup the application pool to run as a user that can access event logs. Hope this helps. PS: I even have color coding! ?
Default.aspx
<html>
<body>
<form runat=server id=form1>
<table class=”style1″>
<tr>
<tr>
<td class=”style2″>
Number of records:</td>
<td>
<asp:DropDownList ID=”DropDownList3″ runat=”server” TabIndex=”0″>
<asp:ListItem Selected=”True”>10</asp:ListItem>
<asp:ListItem>25</asp:ListItem>
<asp:ListItem>50</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<td class=”style2″>
Machine Name:</td>
<td>
<asp:DropDownList ID=”DropDownList2″ runat=”server” TabIndex=”0″>
<asp:ListItem Selected=”True”>ServerX</asp:ListItem>
<asp:ListItem>ServerY</asp:ListItem>
<asp:ListItem>ServerZ</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class=”style2″>
Event Log</td>
<td>
<asp:DropDownList ID=”DropDownList1″ runat=”server” TabIndex=”1″>
<asp:ListItem Selected=”True”>Application</asp:ListItem>
<asp:ListItem>Security</asp:ListItem>
<asp:ListItem>System</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
<asp:Button ID=”Button1″ runat=”server” Text=”Button” />
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”
DataKeyNames=”TimeGenerated” EnableTheming=”False” EnableViewState=”False”
PageIndex=”1″ PageSize=”11″>
<Columns>
<asp:BoundField DataField=”Message” />
<asp:BoundField DataField=”TimeGenerated” HeaderText=”Daetime”
SortExpression=”TimeGenerated” />
<asp:BoundField DataField=”Source” HeaderText=”Source”
SortExpression=”Source” />
<asp:BoundField DataField=”EntryType” HeaderText=”EntryType”
SortExpression=”EntryType” />
</Columns>
</asp:GridView>
<asp:DataGrid id=”LogGrid” runat=”server”
BorderColor=”black”
BorderWidth=”1″
GridLines=”Both”
CellPadding=”3″
CellSpacing=”0″
Font-Name=”Verdana”
Font-Size=”8pt”
HeaderStyle-BackColor=”#aaaadd”
AutoGenerateColumns=”False”
PageSize=”25″
Font-Names=”Verdana”
>
<Columns>
<asp:BoundColumn HeaderText=”TOF” DataField=”EntryType” />
<asp:BoundColumn HeaderText=”Date/Time” DataField=”TimeGenerated”/>
<asp:BoundColumn HeaderText=”Source” DataField=”Source”/>
<asp:BoundColumn HeaderText=”Message” DataField=”Message”/>
</Columns>
<HeaderStyle BackColor=”#AAAADD”></HeaderStyle>
</asp:DataGrid>
<br />
</form>
</body>
</html>
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = True Then
Response.Write(DropDownList2.Text.ToString() & “<br>”)
Dim aLog As New Diagnostics.EventLog
dim z as integer = DropDownList3.Text
aLog.MachineName = DropDownList2.Text
aLog.Log = DropDownList1.Text
‘LogGrid.DataSource = aLog.Entries
‘LogGrid.DataBind()
‘GridView1.DataSource = aLog.Entries
‘GridView1.DataBind()
Dim y As Integer = 0
Response.Write(“<table border=2 cellpadding=2 width=’100%’>”)
Response.Write(“<b><tr><td>Counter</td><td>TOF</td><td>TimeGenerated</td><td>Source</td><td>Message</td></tr></b>”)
Dim item As Diagnostics.EventLogEntry
For Each item In aLog.Entries
y = y + 1
If y = z Then
Exit For
End If
Response.Write(“<tr>”)
Response.Write(“<td>” & y & “</td>”)
Response.Write(“<td>” & aLog.Entries.Item(aLog.Entries.Count – y).EntryType.ToString() & “</td>”)
Response.Write(“<td bgcolor=’#FFFF99′>” & aLog.Entries.Item(aLog.Entries.Count – y).TimeGenerated.ToString() & “</td>”)
Response.Write(“<td>” & aLog.Entries.Item(aLog.Entries.Count – y).Source.ToString() & “</td>”)
Response.Write(“<td bgcolor=’#FFFF99′>” & aLog.Entries.Item(aLog.Entries.Count – y).Message.ToString() & “</td>”)
Response.Write(“</tr>”)
Next
Response.Write(“</table>”)
End If
End Sub
End Class
Here is the event log class reference on MSDN
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx
Cheers,
Steve