15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

478 ❘ ChaPTer 19 instrumentAtiOn<br />

(continued)<br />

eVenTlog members<br />

Source<br />

CreateEventSource()<br />

DeleteEventSource()<br />

SourceExists()<br />

WriteEntry()<br />

WriteEvent()<br />

Clear()<br />

Delete()<br />

desCriPTion<br />

The Source property specifi es the source of the event entries to write.<br />

The CreateEventSource() creates a new event source <strong>and</strong> a new log fi le, if a<br />

new log fi le is specifi ed with this method.<br />

To get rid of an event source, you can invoke DeleteEventSource().<br />

Before creating an event source, you can verify if the source already exists by<br />

using this element.<br />

Write event log entries with either the WriteEntry() or WriteEvent()<br />

method. WriteEntry() is simpler, because you just need to pass a string.<br />

WriteEvent() is more fl exible, because you can use message fi les that are<br />

independent of the application <strong>and</strong> that support localization.<br />

The Clear() method removes all entries from an event log.<br />

The Delete() method deletes a complete event log.<br />

Creating an event source<br />

Before writing events, you must create an event source. You can use either the CreateEventSource()<br />

method of the EventLog class or the class EventLogInstaller . Because you need administrative privileges<br />

when creating an event source, an installation program would be best for defi ning the new source.<br />

Chapter 17, “Deployment,” explains how to create installation programs.<br />

The following sample verifi es that an event log source named EventLogDemoApp already exists. If it<br />

doesn ’ t exist, an object of type EventSourceCreationData is instantiated that defi nes the source name<br />

EventLogDemoApp <strong>and</strong> the log name ProCSharpLog . Here, all events of this source are written to the<br />

ProCSharpLog event log. The default is the application log.<br />

string logName = "ProCSharpLog";<br />

string sourceName = "EventLogDemoApp";<br />

if (!EventLog.SourceExists(sourceName))<br />

{<br />

var eventSourceData = new EventSourceCreationData(sourceName,<br />

logName);<br />

}<br />

EventLog.CreateEventSource(eventSourceData);<br />

code snippet EventLogDemo/Program.cs<br />

The name of the event source is an identifi er of the application that writes the events. For the system<br />

administrator reading the log, the information helps in identifying the event log entries to map them to<br />

application categories. Examples of names for event log sources are LoadPerf for the performance monitor,<br />

MSSQLSERVER for Microsoft SQL Server, MsiInstaller for the Windows Installer, Winlogon , Tcpip ,<br />

Time - Service , <strong>and</strong> so on.<br />

Setting the name Application for the event log writes event log entries to the application log. You can also<br />

create your own log by specifying a different application log name. Log fi les are located in the directory<br />

< windows > \System32\WinEvt\Logs .<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!