This documentation is archived and is not being maintained.

EventLog.CreateEventSource Method

Establishes an application as able to write event information to a particular log on the system.

Overload List

Establishes an application, using the specified Source, as a valid event source for writing entries to a log on the local computer. This method can also create a new custom log on the local computer.

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

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(String, String, String)
[C#] public static void CreateEventSource(string, string, string);
[C++] public: static void CreateEventSource(String*, String*, String*);
[JScript] public static function CreateEventSource(String, String, String);

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, C#, C++] Note   This example shows how to use one of the overloaded versions of CreateEventSource. For other examples that might be available, see the individual overload topics.
[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.

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace

Show: