Gets or sets the application name (source name) to register and use when writing to the event log.
[Visual Basic] Overridable Public Property Source As String [C#] public string Source {virtual get; virtual set;} [C++] public: __property virtual String* get_Source(); public: __property virtual void set_Source(String*); [JScript] public function get Source() : String; public function set Source(String);
The application name registered with the event log as a valid source of entries. The default is an empty string ("").
The event Source is the name of the software that logs the event. It is often the name of the application, or the name of a subcomponent of the application, if the application is large. Applications and services should write to the Application log or a custom log. Device drivers should add their names to the System log.
If you write to an event log, you must specify or create an event Source. The Source, which can only be used to write to a single log at a time, registers your application with the event log as a valid source of entries. The Source can be any random string, but the name cannot be used by other sources on the computer; an attempt to create a duplicated Source value will throw an exception. However, a single event log can have many different sources writing to it.
Note Source names cannot be hierarchical. That is, you cannot use the backslash "\" character.
It is not necessary to specify a Source when only reading from a log. You can specify only the Log name and MachineName (server computer name) for the EventLog instance. In either case, the Entries member is automatically populated with the event log's list of entries. You can select the appropriate index for an item in this list to read individual entries.
Note You are not explicitly required to specify the MachineName if you are connecting to a log by specifying a Log/ MachineName pair. If you do not specify the MachineName, the local computer (".") is assumed.
The Source value can be an empty string, particularly if you are only reading from the event log. If the Source value changes, the event log to which it is registered is closed and all event handles are released.
If you do not specify a log name when calling CreateEventSource, your source will be registered to the Application log. If you specify the name of a log that doesn't exist, the system creates a custom event log for you and registers the Source to that log.
When you write a log entry using WriteEntry, the system uses the source you identified to find the appropriate log in which to place your entry.
Note If you try to call WriteEntry without first having registered the value specified by your EventLog instance's Source parameter as a valid source, Visual Studio automatically registers your component, using the Source value as the source string.
The following example connects to an event log, "myNewLog", on the local computer. If the source "myNewSource" does not already exist on the computer, it is created. Then, an entry is written to the Log.
Note that the way to create a new event log is through the static (in Visual Basic Shared) member CreateEventSource, which creates a Source and registers it to the Log at the time the log is created. Because this is a static (Shared) member of EventLog, it should be called on the class itself rather than on an instance of the class.
Import the System.Diagnostics namespace for this example.
[Visual Basic]
Private Sub ConnectToEventLog() 'Declare a new event log and event log entry. Dim evtLog As EventLog 'Used in a for loop iterating through entries() Dim i As Integer 'Instantiate a new event log. evtLog = New EventLog 'Otherwise, create a new source and log. If Not EventLog.SourceExists("myNewSource") Then 'If MyNewLog does not exist, it will be created EventLog.CreateEventSource("myNewSource", "myNewLog") End If 'Connect the source to the event instance. evtLog.Source = "myNewSource" 'Write an entry to the event log. evtLog.WriteEntry("My entry.") End Sub
EventLog Class | EventLog Members | System.Diagnostics Namespace | Log | MachineName | CreateEventSource | DeleteEventSource | SourceExists