WEBSITE

ASP.NET 4 in VB 2010 : Logging Exceptions (part 4) - Retrieving Log Information

12/18/2013 1:19:24 AM

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).

Figure 6. A log viewer page

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()
Other  
  •  Sharepoint 2010 : Composite Applications with Business Connectivity Services - Getting Started with BCS (part 2) - Creating an External List in SharePoint, Adding Custom Actions to an External Data Li
  •  Sharepoint 2010 : Composite Applications with Business Connectivity Services - Getting Started with BCS (part 1) - Creating an External Content Type
  •  Sharepoint 2010 : Composite Applications with Business Connectivity Services - BCS Components
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 5) - DEVELOPING WINDOWS AZURE APPLICATIONS - Creating a Model
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 4) - DEVELOPING WINDOWS AZURE APPLICATIONS - Creating Your First Windows Azure Application
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 3) - DEVELOPING WINDOWS AZURE APPLICATIONS - Setting Up Your Development Environment
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 2) - WINDOWS AZURE PLATFORM
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 1) - DEFINING THE CLOUD, DEFINING WINDOWS AZURE
  •  Windows Small Business Server 2011 : Customizing a SharePoint Site - Customizing Companyweb (part 5) - Adding RemoteApps Links - Add the Web Part to Companyweb
  •  Windows Small Business Server 2011 : Customizing a SharePoint Site - Customizing Companyweb (part 4) - Adding RemoteApps Links - Register the Web Part as Safe
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone