Export (0) Print
Expand All

EventLog.CreateEventSource Method (String, String, String)

.NET Framework 1.1

Establishes an application, using the specified Source, as a valid event source for writing entries to a log on the computer specified by machineName. This method can also be used to create a new custom log on the specified 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 source by which the application is registered on the specified computer.
logName
The name of the log 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 (Nothing in Visual Basic) or is an empty string ("").
ArgumentException The source cannot be registered because it already exists on the specified computer.
Exception The application cannot open the registry key for the source on the specified computer.

Remarks

Use this overload to create a custom log or to create and register a Source to an existing log on the specified computer.

If the logname is a null reference (Nothing in Visual Basic) when you call CreateEventSource, the log defaults to the Application log. 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 writing an entry to an event log, you must register the event source 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 might either specify the Source, or a Log and MachineName.

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.

Note   If a source has already been mapped to a log and you remap it to a new log, you must reboot the computer for the changes to take effect.

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

Example

[Visual Basic, C#, C++] The following example writes an entry to an event log, "MyNewLog", on the computer "MyServer", and creates the source "MySource" if the source does not already exist.

[Visual Basic, C#, C++] Note   It is not necessary for you to create the event source in your code. If the source you set in the Source property does not exist, the WriteEntry method creates it before writing to the event log. If you do not specify the Log property of the EventLog instance, the log defaults to the Application log.
[Visual Basic] 
Imports System
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create the source, if it does not already exist.
        If Not EventLog.SourceExists("MySource", "MyServer") Then
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer")
            Console.WriteLine("CreatingEventSource")
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
        
        Console.WriteLine("Message written to event log.")
    End Sub ' Main
End Class ' MySample

[C#] 
using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
    
        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource", "MyServer")){
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer");
            Console.WriteLine("CreatingEventSource");
        }
                
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";
        
        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
        
        Console.WriteLine("Message written to event log.");                                                                        
    }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;

int main(){

    // Create the source, if it does not already exist.
    if(!EventLog::SourceExists(S"MySource", S"MyServer")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog", S"MyServer");
        Console::WriteLine(S"CreatingEventSource");
    }

    // Create an EventLog instance and assign its source.
    EventLog* myLog = new EventLog();
    myLog->Source = S"MySource";

    // Write an informational entry to the event log.    
    myLog->WriteEntry(S"Writing to event log.");

    Console::WriteLine(S"Message written to event log.");                                                                        
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.CreateEventSource Overload List | DeleteEventSource | SourceExists | MachineName | Source | Log | Delete

Show:
© 2015 Microsoft