Export (0) Print
Expand All

EventLog.CreateEventSource Method (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 local computer. This method can also 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
);

Parameters

source
The source name by which the application is registered on the local 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.

Exceptions

Exception Type Condition
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 local computer.
Exception The application cannot open the registry key for the source on the local computer.

Remarks

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

If the logname is a null reference (Nothing in Visual Basic) or an empty string ("") when you call CreateEventSource, the log defaults to the Application log. 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.

Note    CreateEventSource defines the new log, but WriteEntry creates the log the first time you call it.

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 can either specify the Source, or a Log and MachineName.

Note   You are not required to specify the MachineName if you are connecting to a log. If you do not specify the MachineName when reading from a log, the local computer (".") is 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.

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 local computer, 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] 
Option Explicit
Option Strict

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") Then
            EventLog.CreateEventSource("MySource", "MyNewLog")
            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.")
    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")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            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.");
        
    }
}
   

[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")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog");
        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.");

}

[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 | Source | Log | Delete

Show:
© 2014 Microsoft