Response.write saves the day. Display the last X number of event log records using ASP.NET on a remote machine.

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

3 thoughts on “Response.write saves the day. Display the last X number of event log records using ASP.NET on a remote machine.

  1. http://

    Data binding an event log to a GridView works fine on my machine.  If you want to enable paging and sorting, you will need to use an ObjectDataSource to encapsulate your event log entries instead.

  2. http://

    Using Response.Write from the Page_Load event will write your content *before* the page content, so your table will come before the opening HTML and BODY tags.

  3. steve schofield

    I’d rather use the datagrid or gridview to bind against. They both error and I have no idea the syntax to use ObjectDataSource. Formatting is good enough using response.write. This is an internal app granting access to application / system event logs. If someone would like to post code how to use the ObjectDataSource, that would be appreciated. :) I had the code in the button on_click event added to the Page_Load event and forgot to change back. It worked so I left it.

Comments are closed.