The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

In this procedure, you will use the SourceExists method to verify that the source you are using does not already exist, and then you will call the CreateEventSource method with the name of a log that does not exist. Because this log does not exist, the system will create a custom log for you when this code is run.

To create the custom log

In the Visual J# version, in the Code Editor, add an import statement to the beginning of the file to reference the System.Diagnostics namespace.

Type in the following code. The event log and source are created as a pair, and the source cannot already exist before the event log is created. After creating the event log, the EventLog component is configured to access the new event log.

When you create an event log, you need to decide what to do if that resource already exists. Another process, perhaps a malicious one, may have already created the event log and have access to it. When you put data in the event log, the data is available to the other process.

In this procedure, you will use the EventLog component instance you created to write entries to the log. To do so, you will first configure the component to use the source string you just created. Then you will specify two entries to write: an informational event and an error event.

To write entries to the log

In the Code Editor, locate the WriteEntry_Click procedure.

(Visual J#) Set the Source property for the EventLog component instance to Source1, which is the source string you used when you created your custom log. Use this code:

Type in the following code. This code uses the overloaded WriteEntry method to write to the event log. The second form shown allows you to specify the type of message. If you view the entries using the Server Explorer in the "Testing Your Code" section below, the different types of entries are indicated by different icons.

In this procedure, you will create two procedures: one that verifies the existence of the custom log, and one that verifies the existence of your source string. These procedures will be used to test the results of various actions you perform when you run the project.

To verify that the custom log exists

In the Code Editor, locate the VerifyLog_Click procedure.

Create a message box that evaluates whether the specified event log exists and displays true or false accordingly. Use this code:

To test writing entries to and reading entries from the custom log

You should now see that two entries have been added to the log. One will have an icon indicating that it is an informational entry, and one will have an icon indicating that it is an error.

Return to the form, and click the Read Entry button.

You will receive two prompts: one containing the informational entry and one containing the error entry.

Note

If you clicked the Write Entry button multiple times, you will have received more prompts.

To test clearing the log

In the form, click the Clear Log button.

In Server Explorer, right-click the NewLog1 node, and then click Refresh.

You should now see that the log no longer contains entries.

To test removing the source string

In the form, click the Remove Event Source button.

Click the Verify Source Exists button. You should receive a prompt that says "False" to indicate that the source Source1 no longer exists.

Click the Write Entry button.

Note

This action will allow you to write entries to the log, because the WriteEntry method will set the source if it does not currently exist.

In Server Explorer, right-click the NewLog1 node, and then click Refresh. You should see two entries in the log.

Click the Verify Source Exists button again. You should receive a prompt that says "True" to indicate that the source Source1 exists.

Optionally, you may want to click the DeleteLog button when you are through testing. This will remove the log NewLog1 and the source Source1 so that there are no permanent changes to your system's configuration.