Establishes an application as an event source with the specified Source. Registers the source to a Log that resides on the local computer. This method can also be used to create a new custom log on the local computer.
[Visual Basic] Overloads Public Shared Sub CreateEventSource( _ ByVal source As String, _ ByVal logName As String _ ) [C#] public static void CreateEventSource( string source, string logName ); [C++] public: static void CreateEventSource( String* source, String* logName ); [JScript] public static function CreateEventSource( source : String, logName : String );
Exception Type | Condition |
---|---|
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 local computer. |
Exception | The application can't open the registry key for the source on the local computer. |
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 an existing log on the local computer.
If you do not specify a logname when calling CreateEventSource, it defaults to Application. If the Log does not exist on the local 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 you 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 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 local computer. 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 call WriteEntry.
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 '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("Event log entry.") End Sub
EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.CreateEventSource Overload List | DeleteEventSource | SourceExists | Source | Log | Delete