programming4us
programming4us
WEBSITE

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

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

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
  •  
    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