Source Property
Collapse the table of content
Expand the table of content

EventLog.Source Property

Gets or sets the source name to register and use when writing to the event log.

[Visual Basic]
Public Property Source As String
[C#]
public string Source {get; set;}
[C++]
public: __property String* get_Source();
public: __property void set_Source(String*);
[JScript]
public function get Source() : String;
public function set Source(String);

Property Value

The name registered with the event log as a source of entries. The default is an empty string ("").

Remarks

The event source indicates what logs the event. It is often the name of the application, or the name of a subcomponent of the application, if the application is large. Applications and services should write to the Application log or a custom log. Device drivers should write to the System log.

If you write to an event log, you must specify or create an event source, and set the Source property appropriately. If you create a new source, which can only write to one log at a time, it registers your application with the event log as a valid source of entries. The Source property can be any string, but the name cannot be used by other sources on the computer. An attempt to create a duplicated Source value throws an exception. However, a single event log can have many different sources writing to it.

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.

It is not necessary to specify the Source property when only reading from a log. You can specify only the Log name and MachineName (server computer name) for the EventLog. In either case, the Entries member is automatically populated with the event log's list of entries.

Note   You are not required to specify the MachineName if you are connecting to a log. If you do not specify the MachineName, the local computer (".") is assumed.

The Source value can be an empty string if you are only reading from the event log. If the Source value changes, the EventLog to which it is registered is closed and all event handles are released.

If you do not specify a log name when calling CreateEventSource, your source will be registered to the Application log. If you specify the name of a log that does not exist, the system creates a custom event log for you and registers the Source to that log.

When you write a log entry using WriteEntry, the system uses the source you identified to find the appropriate log in which to place your entry.

Note   If you call WriteEntry without first having registered the Source for your EventLog, the method registers the source for you.

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 already exist, the WriteEntry method creates it before writing to the event log. If you do not specify the Log property of the EventLog, 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.")
        
        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")){
            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.");
        
        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")){
        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.");

    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 | Log | MachineName | CreateEventSource | DeleteEventSource | SourceExists

Show:
© 2016 Microsoft