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.
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:
Run regedit.exe, either by using a command-line prompt or by choosing Run from the Start menu. Browse to the HKEY_Local_Machine\SYSTEM\CurrentControlSet\Services\EventLog section of the registry. 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. Right-click the folder and choose Permissions. 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. Give the account Full Control for this section of the registry by selecting the Allow check box next to Full Control.
|