NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

EventLog.CreateEventSource (String, String, String)

Establishes an application as an event source with the specified Source. Registers the source to a Log that resides on the computer specified by machineName. This method can also be used to create a new custom log on the given computer.

[Visual Basic]
Overloads Public Shared Sub CreateEventSource( _
   ByVal source As String, _
   ByVal logName As String, _
   ByVal machineName As String _
)
[C#]
public static void CreateEventSource(
   string source,
   string logName,
   string machineName
);
[C++]
public: static void CreateEventSource(
   String* source,
   String* logName,
   String* machineName
);
[JScript]
public static function CreateEventSource(
   source : String,
   logName : String,
   machineName : String
);

Parameters

source
The name the application is registered with on the specified computer.
logName
The name of the log that the source's entries are written to. Possible values include: Application, Security, System, or a custom event log. If you do not specify a value, the logName defaults to Application.
machineName
The name of the computer to register this event source with, or "." for the local computer.

Exceptions

Exception Type Condition
ArgumentException The machineName parameter is invalid.
ArgumentException The source parameter is a null reference (in Visual Basic Nothing) or is an empty string ("").
ArgumentException The source can't be registered because it already exists on the specified computer.
Exception The application can't open the registry key for the source on the specified computer.

Remarks

Use CreateEventSource both to create a Source and register it to a new or existing log, and also to create a new custom log. Use this overload to create a custom log or to create and register a Source to a log on any server on the network.

If you do not specify a logname when calling CreateEventSource, it defaults to Application. If the Log does not exist on the specified computer, the system creates a custom log and registers your application as a Source for that log.

You only need to create an event source if you are writing to the event log. Before your component can write an entry to an event log, you must register it with the event log as a valid source of events. When you write a log entry using WriteEntry, the system uses the Source to find the appropriate log in which to place your entry. If you are reading the event log, you may either specify the Source, or you may specify a Log/ MachineName pair.

Note   If you do not specify a MachineName when reading from a log, the local computer (".") will be assumed.

The source parameter can be any string, although often it will be the name of your application or a component of your application. The source must be unique on the server specified by the machineName parameter. However, a single event log can have many different sources writing to it at once.

Calling this method is optional; if you do not, it is called the first time you use WriteEntry.

Example [Visual Basic]

The following example connects to an event log, "myNewLog", on the computer "myComputer". 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

    'Instantiate a new event log.
    evtLog = New EventLog

    'Otherwise, create a new source and log.
    If Not EventLog.SourceExists("myNewSource", "myComputer") Then
        'If MyNewLog does not exist, it will be created
        EventLog.CreateEventSource("myNewSource", "myComputer", "myNewLog")
    End If
    
    'Connect the source to the event instance.
    evtLog.Source = "myNewSource"
    
    'Write an entry to the event log.
    evtLog.WriteEntry("Event log entry.")
End Sub

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.CreateEventSource Overload List | DeleteEventSource