Creating Log Files
At one time, administrators relied on text log files to store information from applications. However,
most applications today output complex information that’s hard to read within a text file. If you plan
to create log files for your application, you probably want to store them in XML format to make them
easy to ready and easy to import into a database. Chapter 13 describes how to work with XML files.
548592c10.indd 220
2/24/10 12:48:34 PM
www.finebook.ir
Providing Status Information
❘
221
Using the Event Log
Many applications rely on the event log as a means to output data to the administrator. Of all of the
methods that Microsoft has created for outputting error and status information, the event log has
been around the longest and is the most successful. Fortunately, for the IronPython developer, using
the event log is extremely easy and it’s the method that you should use most often. Listing 10-11
shows just how easy it is to write an event log entry.
LISTINg 10-11:
Writing an event log entry
# Import the required assemblies.
from System.Diagnostics import EventLog, EventLogEntryType
# Create the event log entry.
ThisEntry = EventLog(‘Application’, ‘Main’, ‘SampleApp’)
# Write data to the entry.
ThisEntry.WriteEntry(‘This is a test!’, EventLogEntryType.Information)
# Pause after the debug session.
raw_input(‘Event log entry written...’)
The
EventLog()
constructor accepts a number of different inputs. The form shown in the example
defines the log name, machine name, and the application name. In most cases, this is all the infor-
mation you need to start writing event log entries.
After you create
ThisEntry
, you can use it to begin writing event log entries as needed using the
WriteEntry()
method. The
WriteEntry()
is overloaded to accept a number of information for-
mats — the example shows what you’ll commonly use for simple entries. You can see other forms
of the
WriteEntry()
method at
http://msdn.microsoft.com/library/system.diagnostics
.eventlog.writeentry.aspx
.
In this case, the
WriteEntry()
provides a message and defines the kind of event log entry to create.
You can also create warning, error, success audit, and failure audit messages. Figure 10-16 shows
the results of running this example.
FIgURE 10-16:
The example outputs data to the event log.
548592c10.indd 221
2/24/10 12:48:34 PM
www.finebook.ir
Do'stlaringiz bilan baham: |