programming4us
programming4us
WEBSITE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us