CSCI E-143 CertPrep:   Maintaining   (Kalani,  chapter 14)

Saturday,  Dec. 18, 2004

This Saturday we cover Kalani's chapter 14,  "Maintaining and Supporting a Windows Application".

There are three classes covered by this chapter:     Process, EventLog, PerformanceCounter

There is a namespace common to all three:     System.Diagnostics.


The  "Process"  Class

You use the  Process  Class to get information about other things running on the local machine or on a remote machine,  and to start and stop routines on the local machine.

Starting and stopping processes

Methods of the  Process  Class for controlling processes

Properties of the  Process  Class

Events of the  Process  Class

There is an example of using these in Kalani,  on pages 946-948.

Getting information about Processes

Kalani has a long list of properties of  Processes on page 949.   Interesting,  but not worth memorizing.


Event Logs

Event Logs are persistent lists of messages put out by programs as they run.   They can be easily examined by system tools,  on this machine or another connected to it,  and they can be read from code.   Kalani doesn't mention that they can also contain data other than text strings.

The easiest way to see these logs is through the .NET IDE,  under Server Explorer.   You can also see them using Control Panel / Administrative Tools / Event Viewer.

There are three types of logs available by default on your machine:  Application,  Security,  and System.   The Security log is read-only.

The reason for the presence of these several pages in Kalani,  or for any involved questions on the test,  is puzzling.   It is perfectly easy to create an event log source and use it in your program to write to a log—even to deploy it on a target machine—by using the IDE's tools.   Specifically,  you drag the EventLog you want to use from Server Explorer,  give it a source,  and write to it.   However,  we must look at what Kalani has written.

You can write logs on your own machine.   You can read and write logs on remote machines,  but you cannot receive notifications—i.e.,  events—for log entries written there.

Kalani ducks the issue of creating an entirely new log—that is,  a log in addition to Application,  Security,  and System.

"Source"

Each log has a source.   "If you write to an event log,  you must specify or create an event Source.   The Source registers your application with the event log as a valid source of entries.   You can only use the Source to write to one log at a time.   The Source can be any random string,  but the name must be distinct from other sources on the computer.   It is common for the source to be the name of the application or another identifying string.   An attempt to create a duplicated Source value throws an exception.   However,  a single event log can be associated with multiple sources.... A source is required only for writing to logs" [not for reading them]     (Help: "EventLog Class, about")
"If a source has already been mapped to a log and you remap it to a new log, you must reboot the computer for the changes to take effect."     (Help: "EventLog.Source Property")

Don't be misled by the .NET IDE!   If I open Server Explorer on my machine,  one of the nodes I find is labelled  "Event Logs".   If I expand that node,  I find three subnodes:  Application,  Security,  and System.   If I expand the Application node,  I will get,  on my machine,  a long list beginning

What nobody seems to make quite clear is that every entry listed here under  "Application"  is not a separate  log  but a separate  source.   You get what appears in this listing to be a separate log for every such source.   (Other tools,  such as the System's Event Viewer,  do not list the Event Logs this way,  but give the source as one column in a display,  not as a separate node.)

Writing to a log from code

It is easy to write to a log from code.   Don't be fooled by all the code listed in this chapter.   Here's what you have to do:

Details

Some methods of the  EventLog  Class:

Some properties of the  EventLog  Class:

The  EventLogEntryType  Enumeration.   (An optional descriptor for each entry written to a log):

Event Log Annoyances


Performance Counters

Performance counters are numerical values put out by a program for diagnostic purposes.   They can be written and read from code,  and they can be read and their changes over time graphed by system tools such as  perfmon.exe.

Terms and Annoyances

Category.   Performance counters are grouped into categories.   You can only add a performance counter to a new Category when you create it.   You cannot add performance counters to an existing Category.    (Kalani, box, p. 970)

Instance,  as in "Instance counter".   Pay attention:  this is a really misleading use of object-oriented terminology,  even by Microsoft standards:

An "instance counter" is not an instance of a performance counter,  as every other object of a class would lead you to expect.   An "instance counter" is a counter for an instance of something else.   For example,  if you have a number of copies of the same routine running—say,  several copies of Notepad—they all might have their own set of counters.   That is,  each  instance  of Notepad would have its own set of  "instance counters".   Clear?    (Kalani, p. 965)

Sampling.   The term for reading the values of performance counters.   (Kalani, p. 967)

Publishing.   The term for setting values into performance counters.   You can only read, not set, counters on a remote machine.   (Kalani, p. 969)

perfmon.exe.   A system tool for monitoring these counters.   You can start it from  Start / Run  or from  Windows Control Panel / Administrative Tools.

RawValue.   The value for an uncalculated number set for a performance counter.

BaseValue.   Another value set for a performance counter,  this one used in calculating values to display in the monitor tool.

Some members in the Performance Counter Class

Reading a Performance Counter from code

Here's what you have to do:

There is a rather complex example of reading Performance Counters,  including instance counters,  at the end of this chapter.   (Kalani, pp. 980-982)     This is the only place where Kalani really addresses the problem of instance counters.

A source of further information on Performance Counters is Burton.   (Burton, pp. 727-757)


Free Advice

Kalani ends this chapter with a few pages on good performance practice—i.e.,  code that produces fast-running programs.   I won't repeat the list here,  as most of it is obvious.   For the entry labelled "Use auto-generated commands",  though,  he seems to have omitted the word "don't".   (Incidentally,  this is the only mention anywhere in Kalani of the  CommandBuilder  class.   Remember  CommandBuilder?)

A few items on this list pop up in the sample test questions.   I can't remember if any of them occurred on the real test.

(In general,  Kalani is quite free from this sort of mush.   This section and the one on  "Logo Programs"  at the end of chapter 13 are about the only examples I can remember.)


The  ListView  Control

Incidentally,  notice the frequent use of the  ListView  control in this chapter.   There are examples on pages 952,  962,  968,  978,  and 980.   What are the differences between a  ListView  control and a DataGrid?   Well,  for one thing,  the columns in a  ListView  seem to be of variable width by default.  


The  (parenthesized references)  in the above list are explained  here.
Here is the relevant question from the two tests at the end of the "short Kalani"  book.   (The fact that there's only one question should tell you something.)

    Test 1:  question  57  (page 383;  answer:  page 399).

    Test 2:  none.

(The "short Kalani" text is listed here.)


Last  revised  Dec. 19, 2004