WEBSITE

ASP.NET 4 in VB 2010 : Logging Exceptions (part 2) - Writing to the Event Log

12/18/2013 1:17:17 AM

2. Writing to the Event Log

You can interact with event logs in an ASP.NET page by using the classes in the System.Diagnostics namespace. First, import the namespace at the beginning of your code-behind file:

Imports System.Diagnostics

The following example rewrites the simple ErrorTest page to use event logging:

Public Partial Class ErrorTestLog
Inherits System.Web.UI.Page

Protected Sub cmdCompute_Click(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles cmdCompute.Click


Try
Dim A, B, Result As Decimal
A = Decimal.Parse(txtA.Text)
B = Decimal.Parse(txtB.Text)
Result = A / B
lblResult.Text = Result.ToString()
lblResult.ForeColor = System.Drawing.Color.Black
Catch err As Exception
lblResult.Text = "<b>Message:</b> " & err.Message & "<br /><br />"
lblResult.Text &= "<b>Source:</b> " & err.Source & "<br /><br />"
lblResult.Text &= "<b>Stack Trace:</b> " & err.StackTrace
lblResult.ForeColor = System.Drawing.Color.Red

' Write the information to the event log.
Dim Log As New EventLog()
Log.Source = "DivisionPage"
Log.WriteEntry(err.Message, EventLogEntryType.Error)
End Try
End Sub

End Class


The event log record will now appear in the Event Viewer utility, as shown in Figure 4. Note that logging is intended for the system administrator or developer. It doesn't replace the code you use to notify the user and explain that a problem has occurred.

Figure 4. An event record

EVENT LOG SECURITY

There's a potential problem with this example. Ordinarily, Windows won't allow you to access the event log, and you'll get an exception when you attempt to run this example. The proper way to solve this problem depends on whether you're testing your web application or deploying it to a live web server.

If you're just testing your logging code, the easiest option is to run Visual Studio as an administrator. To do this, right-click the Visual Studio shortcut and choose Run As Adminstrator. This step is necessary because of the user account control (UAC) safety system in Windows. UAC prevents you from using administrator privileges unless you specifically request them, all in a bid to restrict the viruses, malware, and hackers that can attack your computer.

When you deploy your application to a web server, the process isn't quite as simple. Ordinarily, the account that runs your web applications has a carefully limited set of permissions. It definitely won't be an administrator account. If you want your web application to be able to use the event log, you need to give that account the permissions it needs to create event log entries. Here are the steps that you (or an administrator) must follow on the web server computer:

  1. Run regedit.exe, either by using a command-line prompt or by choosing Run from the Start menu.

  2. Browse to the HKEY_Local_Machine\SYSTEM\CurrentControlSet\Services\EventLog section of the registry.

  3. Select the EventLog folder if you want to give ASP.NET permission to all areas of the event log. Or select a specific folder that corresponds to the event log ASP.NET needs to access.

  4. Right-click the folder and choose Permissions.

  5. Add the account that ASP.NET is using to the list (or a group that this account belongs to). If you're using IIS in Windows 7, Windows Vista, or Windows Server 2008, you need to add permissions to the IIS_IUSRS group.

  6. Give the account Full Control for this section of the registry by selecting the Allow check box next to Full Control.

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