Writing to a Custom Event Log
The EventLog service is an excellent component to reuse. By reusing the EventLog service, you avoid the work involved in creating a custom process for logging critical events, and you provide your user community with a reliable, consistent, and anticipated location to check for application problems.
A second benefit of the EventLog service is that you can choose to create a custom log and use it during the development, testing, and debugging of procedural-level details in your application. For example, you can use it to ensure that you handle the possibility of a full event log (see Figure 20.4).
Figure 20.4. Your application should be defined to handle a full event log, as shown.
The code for using a custom event log is almost identical to that for using an existing log. When you create the custom log, specify a new log name when you create the event source. EventLog.CreateEventSource( sourcename , logname ) will create a custom log file, adding a key to the registry and leaf node to the Event Viewer (see Figure 20.5).
Figure 20.5. A custom log, FullLog, shown in the Event Viewer.
Choosing Action, Properties in the Event Viewer allows you to customize individual event logs. If you change the Log Size property to Overwrite Events as Needed, you will avoid exceptions because of full event logs. However, overwriting entries too frequently may make it hard to sift through the log and determine what caused the system to fail (see Figure 20.6 for a view of the log properties).
Figure 20.6. The FullLog Properties Page allows you to change the event log's maximum size and control log overwriting.
Source names may only be used once across all logs. For example, if the source "FullLog" exists in a custom log, you may not create a "FullLog" source in some other log. Check out the EventLogDemo for many examples using the EventLog class and component.