3. Custom Logs
You can also log errors to a custom log.
For example, you could create a log with your company name and add
records to it for all your ASP.NET applications. You might even want to
create an individual log for a particularly large application and use
the Source property of each entry to indicate the page (or web service
method) that caused the problem.
Accessing a custom log is easy—you just need to use
a different constructor for the EventLog class to specify the custom
log name. You also need to register an event source
for the log. This initial step needs to be performed only once—in fact,
you'll receive an error if you try to create the same event source.
Typically, you'll use the name of the application as the event source.
Here's an example that uses a custom log named ProseTech and registers the event source DivideByZeroApp:
' Register the event source if needed.
If Not EventLog.SourceExists("DivideByZeroApp") Then
' This registers the event source and creates the custom log,
' if needed.
EventLog.CreateEventSource("DivideByZeroApp", "ProseTech")
End If
' Open the log. If the log does not exist, it will be created automatically.
Dim Log As New EventLog("ProseTech")
log.Source = "DivideByZeroApp"
log.WriteEntry(err.Message, EventLogEntryType.Error)
If you specify the name of a log that doesn't exist
when you use the CreateEventSource() method, the system will create a
new, custom event log for you the first time you write an entry.
To see a newly created event log in the Event Viewer
tool, you'll need to exit Event Viewer and restart it. Custom logs
appear in a separate group named Applications and Services Logs, as
shown in Figure 5.
You can use this sort of code anywhere in your
application. Usually, you'll use logging code when responding to an
exception that might be a symptom of a deeper problem.
4. A Custom Logging Class
Rather than adding too much logging code in the
Catch block, a better approach is to create a separate class that
handles the event logging grunt work. You can then use that class from
any web page, without duplicating any code.
To use this approach, begin by creating a new code
file in the App_Code subfolder of your website. You can do this in
Visual Studio by choosing Website => Add New Item. In the Add New Item dialog box, choose Class, pick a suitable file name, and then click Add.
Here's an example of a class named MyLogger that handles the event logging details:
Public Class MyLogger
Public Sub LogError(ByVal pageInError As String, ByVal err As Exception)
RegisterLog()
Dim log As New EventLog("ProseTech")
log.Source = pageInError
log.WriteEntry(err.Message, EventLogEntryType.Error)
End Sub
Private Sub RegisterLog()
' Register the event source if needed.
If Not EventLog.SourceExists("ProseTech") Then
EventLog.CreateEventSource("DivideByZeroApp", "ProseTech")
End If
End Sub
End Class
Once you have a class in the App_Code folder, it's
easy to use it anywhere in your website. Here's how you might use the
MyLogger class in a web page to log an exception:
Try
' Risky code goes here.
Catch err As Exception
' Log the error using the logging class.
Dim logger As New MyLogger()
logger.LogError(Request.Path, err)
' Now handle the error as appropriate for this page.
lblResult.Text = "Sorry. An error occurred."
Emd Try
If you write log entries frequently, you may not
want to check whether the log exists every time you want to write an
entry. Instead, you could create the event source once—when the
application first starts up—using an application event handler in the
global.asax file.
Event logging uses disk space and takes processor
time away from web applications. Don't store unimportant information,
large quantities of data, or information that would be better off in
another type of storage (such as a relational database). Generally, you
should use an event log to log unexpected conditions or errors, not
customer actions or performance-tracking information.
|
|