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! :)



<form runat=server id=form1>

<table class=”style1″>
        <td class=”style2″>
            Number of records:</td>
            <asp:DropDownList ID=”DropDownList3″ runat=”server” TabIndex=”0″>
                <asp:ListItem Selected=”True”>10</asp:ListItem>
        <td class=”style2″>
            Machine Name:</td>
            <asp:DropDownList ID=”DropDownList2″ runat=”server” TabIndex=”0″>
            <asp:ListItem Selected=”True”>ServerX</asp:ListItem>
        <td class=”style2″>
            Event Log</td>
            <asp:DropDownList ID=”DropDownList1″ runat=”server” TabIndex=”1″>
                <asp:ListItem Selected=”True”>Application</asp:ListItem>
 <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″>
            <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” />
<asp:DataGrid id=”LogGrid” runat=”server”
      <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”/>

<HeaderStyle BackColor=”#AAAADD”></HeaderStyle>

<br />




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

            ‘GridView1.DataSource = aLog.Entries
            Dim y As Integer = 0
            Response.Write(“<table border=2 cellpadding=2 width=’100%’>”)
            Dim item As Diagnostics.EventLogEntry
            For Each item In aLog.Entries
                y = y + 1
                If y = z Then
                    Exit For
                End If
                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>”)
        End If
    End Sub
End Class

Here is the event log class reference on MSDN



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.