5. Retrieving Log Information
One of the disadvantages of the event logs
is that they're tied to the web server. This can make it difficult to
review log entries if you don't have a way to access the server
(although you can read them from another computer on the same network).
This problem has several possible solutions. One interesting technique
involves using a special administration page. This ASP.NET page can use
the EventLog class to retrieve and display all the information from the
event log.
Figure 6
shows in a simple web page all the entries that were left by the
ErrorTestCustomLog page. The results are shown using a label in a
scrollable panel (a Panel control with the Scrollbars property set to
Vertical).
Here's the web page code you'll need:
Public Partial Class EventReviewPage
Inherits System.Web.UI.Page
Protected Sub cmdGet_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles cmdGet.Click
lblResult.Text = ""
' Check if the log exists.
If Not EventLog.Exists(txtLog.Text) Then
lblResult.Text = "The event log " & txtLog.Text & _
" does not exist."
Else
Dim log As New EventLog(txtLog.Text)
For Each entry As EventLogEntry In log.Entries
' Write the event entries to the page.
If chkAll.Checked Or entry.Source = txtSource.Text Then
lblResult.Text &= "<b>Entry Type:</b> "
lblResult.Text &= entry.EntryType.ToString()
lblResult.Text &= "<br /><b>Message:</b> " & entry.Message
lblResult.Text &= "<br /><b>Time Generated:</b> "
lblResult.Text &= entry.TimeGenerated
lblResult.Text &= "<br /><br />"
End If
Next
End If
End Sub
Protected Sub chkAll_CheckedChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles chkAll.CheckedChanged
' The chkAll control has AutoPostback = True.
If chkAll.Checked Then
txtSource.Text = ""
txtSource.Enabled = False
Else
txtSource.Enabled = True
End If
End Sub
End Class
If you choose to display all the entries from the
application log, the page will perform slowly. Two factors are at work
here. First, it takes time to retrieve each event log entry; a typical
application log can easily hold several thousand entries. Second, the
code used to append text to the Label control is inefficient. Every
time you add a new piece of information to the Label.Text property,
.NET needs to generate a new String object. A better solution is to use
the specialized System.Text.StringBuilder class, which is designed to
handle intensive string processing with a lower overhead by managing an
internal buffer or memory.
Here's the more efficient way you could write the string processing code:
' For maximum performance, join all the event
' information into one large string using the
' StringBuilder.
Dim sb As New System.Text.StringBuilder()
Dim log As New EventLog(txtLog.Text)
For Each entry As EventLogEntry In log.Entries
' Write the event entries to the StringBuilder.
If chkAll.Checked Or entry.Source = txtSource.Text Then
sb.Append("<b>Entry Type:</b> ")
sb.Append(entry.EntryType.ToString())
sb.Append("<br /><b>Message:</b> ")
sb.Append(entry.Message)
sb.Append("<br /><b>Time Generated:</b> ")
sb.Append(entry.TimeGenerated.ToString())
sb.Append("<br /><br />")
End If
Next
' Copy the complete text to the web page.
lblResult.Text = sb.ToString()