Access Event Log Data via the Web

Now that you have all this information stored in the Windows 2000 Event Log, how do you view it? Well, if you’re sitting at the machine or have access to another machine on the same network, then you can use the Windows 2000 Event Log Viewer. If you’re not on the network, then you have to come up with something else. Assuming that you have a secure directory on your website (you wouldn’t want to make your logs public), you can build a web-based viewer to check your web server’s event log. This is because the EventLog class in the System. Diagnostics namespace exposes a complete interface for the manipulation of your event logs. You’ll build a small event log viewer here if you follow along with Listings 8.9 and 8.10.

Listing 8.9. Web-Based Event Log Viewer (C#)
<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Diagnostics" %> 

<script language="C#" runat="server"> 
     protected void Page_Load(object sender, EventArgs e) 
     {
          if (!IsPostBack) 
          {
               EventLog[] elArray = EventLog.GetEventLogs("."); 
               logs.DataSource = elArray; 
               logs.DataTextField = "Log"; 
               logs.DataValueField = "Log"; 
               logs.DataBind(); 
          } 
     } 

     protected void getMessages_Click(object sender, EventArgs e) 
     {
          GetLogEntries(); 
     } 
     protected void clearLog_Click(object sender, EventArgs e) 
     {
          EventLog el = new EventLog(); 
          el.MachineName = "."; 
          el.Log = logs.SelectedItem. ToString(); 
          el.Clear(); 
          el.Close(); 

          GetLogEntries(); 
     } 

     protected void GetLogEntries() 
     {
          EventLog el = new EventLog(); 
          el.MachineName = "."; 
          el.Log = logs.SelectedItem. ToString(); 

          messages.DataSource = el.Entries; 
          messages.DataBind(); 
          el.Close(); 
     } 

     protected string GetEventTypeDesc(EventLogEntryType elet) 
     {
          switch(elet) 
          {
               case EventLogEntryType.Error: 
                    return "Error"; 
                    break; 
               case EventLogEntryType.Warning: 
                    return "Warning"; 
                    break; 
               case EventLogEntryType.Information: 
                    return "Information"; 
                    break; 
               case EventLogEntryType.SuccessAudit: 
                    return "Success Audit"; 
                    break; 
               default: / /EventLogEntryType.FailureAudit 
                    return "Failure Audit"; 
                    break; 
          } 
      } 
</script> 

<html> 
<head> 
<title>Event Log Viewer</title> 
</head> 
<body> 

<form runat="server"> 
           <asp:dropdownlist id="logs" runat="server" /> 
           <asp:button id="getMessages" text="Get Log Entries" 
                     onclick="getMessages_Click" runat="server" /> 
           <asp:button id="clearLog" text="Clear Log Entries" 
                     onclick="clearLog_Click" runat="server" /> 
</form> 

<asp:repeater id="messages:" runat="server"> 
         <HeaderTemplate> 
            <table border="1" cellspacing="0" cellpadding="2"> 
                 <tr> 
                       <th>Type</th> 
                       <th>Date/Time</th> 
                       <th>Source</th> 
                       <th>Category</th> 
                       <th>Event</th> 
                       <th>User</th> 
                       <th>Computer</th> 
                       <th>Message</th> 
                 </tr> 
        </Headertemplate> 
        <ItemTemplate> 
             <tr> 
                  <td> 
                         <%#GetEventTypeDesc(
                         ((EventLogEntry)Container.DataItem).EntryType)%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem) 
                         .TimeGenerated. ToString("G",null)%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem).Source%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem).Category%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem).EventID%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem).UserName%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem).MachineName%> 
                  </td> 
                  <td> 
                         <%#((EventLogEntry)Container.DataItem).Message%> 
                  </td> 
            </tr> 
      </Itemtemplate> 
      <FooterTemplate"> 
            </table> 
      </Footertemplate> 
</asp:repeater> 
</body> 
</html> 

Listing 8.10. Web-based Event Log Viewer (Visual Basic .NET)
<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Diagnostics" %> 

<script language="VB" runat="server"> 
      Protected Sub Page_Load(sender As Object, e As EventArgs) 
           If Not IsPostBack Then 
                Dim elArray() As EventLog = EventLog.GetEventLogs(".") 
                With logs 
                     .DataSource = elArray 
                     .DataTextField = "Log" 
                     .DataValueField = "Log" 
                     .DataBind() 
                End With 
           End If 
      End Sub 

      Protected Sub getMessages_Click(sender As Object, e As EventArgs) 
           GetLogEntries() 
      End Sub 

      Protected Sub clearLog_Click(sender As Object, e As EventArgs) 
           Dim el As EventLog = New EventLog() 
           el.MachineName = "." 
           el.Log = logs.SelectedItem. ToString() 
           el.Clear() 
           el.Close() 

           GetLogEntries() 
      End Sub 

      Protected Sub GetLogEntries() 
           Dim el As EventLog = New EventLog() 
           With el 
                .MachineName = "." 
                .Log = logs.SelectedItem. ToString() 
           End With 

           messages.DataSource = el.Entries 
           messages.DataBind() 
      End Sub 

      Protected Function GetEventTypeDesc(elet As EventLogEntryType) _ 
           As String 
           Select Case elet 
                Case EventLogEntryType.Error 
                     return "Error" 
                Case EventLogEntryType.Warning 
                     return "Warning" 
                Case EventLogEntryType.Information 
                     return "Information" 
                Case EventLogEntryType.SuccessAudit 
                     return "Success Audit" 
                Case Else 'EventLogEntryType.FailureAudit 
                     return "Failure Audit" 
           End Select 
      End Function 
</script> 

<html> 
<head> 
<title>Event Log Viewer</title> 
</head> 
<body> 

<form runat="server"> 
    <asp:dropdownlist id="logs" runat="server" /> 
    <asp:button id="getMessages" text="Get Log Entries" 
          onclick="getMessages_Click" runat="server" /> 
    <asp:button id="clearLog" text="Clear Log Entries" 
          onclick="clearLog_Click" runat="server" />              </form> 
</form> 

<asp:repeater id="messages" runat="server"> 
     <HeaderTemplate> 
          <table border="1" cellspacing="0" cellpadding="2"> 
                <tr> 
                    <th>Type</th> 
                    <th>Date/Time</th> 
                    <th>Source</th> 
                    <th>Category</th> 
                    <th>>Event</th> 
                    <th>User</th> 
                    <th>Computer</th> 
                    <th>Message</th> 
                </tr> 
     </Headertemplate> 
     <ItemTemplate> 
            <tr> 
                <td> 
                    <%#GetEventTypeDesc(Container.DataItem.EntryType)%> 
                </td> 
                <td> 
                   <%#Container.DataItem. TimeGenerated. ToString("G")%> 
              </td> 
              <td> 
                   <%#Container.DataItem.Source%> 
              </td> 
              <td> 
                   <%#Container.DataItem.Category%> 
              </td> 
              <td> 
                   <%#Container.DataItem.EventID%> 
              </td> 
              <td> 
                   <%#Container.DataItem.UserName%> 
              </td> 
              <td> 
                   <%#Container.DataItem.MachineName%> 
              </td> 
              <td> 
                   <%#Container.DataItem.Message%> 
              </td> 
            </tr> 
      </Itemtemplate> 
      <FooterTemplate> 
            </table> 
      </Footertemplate> 
</asp:repeater> 
</body> 
</html> 

In this sample, when the page first loads, you build and display an ASP.NET DropDownList server control that contains the names of all the event logs on the local server. This list is obtained by calling the Static GetEventLogs method of the EventLog class and passing in the . wildcard character that stands for local server. Alternatively, you could specify another machine on the network. The GetEventLogs method returns an array of EventLog objects that you bind to the DropDownList server control.

If you select one of the event logs from the DropDownList control and click the Get Log Entries button, the getMessages_Click server event is fired. This event calls the GetLogEntries function that you define. The GetLogEntries function gets a list of event log entries for the selected event log. It then binds this collection of EventLogEntry objects to the messages Repeater server control. The messages Repeater displays the properties of each EventLogEntry object in an HTML table. The function then calls the Close method of the EventLog object instance .

A Clear Log Entries button also calls the clearLog_Click event. Inside this event, you establish a connection to the event log that was selected in the DropDownList server control and call the Clear method of the EventLog object instance. You then call the Close method. Finally, you call the GetLogEntries function to refresh the Repeater server control. The web-based Event Log Viewer with some sample results will look similar to Figure 8.3.

Figure 8.3. You can programmatically re-create the Windows 2000 Event Log in the form of an ASP.NET page.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset