Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

EventLog.Source Property

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

Namespace: System.Diagnostics
Assembly: System (in system.dll)

[TypeConverterAttribute("System.Diagnostics.Design.StringValueConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] 
public string Source { get; set; }
/** @property */
public String get_Source ()

/** @property */
public void set_Source (String value)

public function get Source () : String

public function set Source (value : String)

Not applicable.

Property Value

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

Exception typeCondition

ArgumentException

The source name results in a registry key path longer than 254 characters.

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.

You only need to specify an event source if you are writing to an 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, the system uses the Source property 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.

NoteNote:

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

Use WriteEvent and WriteEntry to write events to an event log. You must specify an event source to write events; you must create and configure the event source before writing the first entry with the source.

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

You can create an event source for an existing event log or a new event log. When you create a new source for a new event log, the system registers the source for that log, but the log is not created until the first entry is written to it.

The source must be unique on the local computer; a new source name cannot match an existing source name or an existing event log name. Each source can write to only one event log at a time; however, your application can use multiple sources to write to multiple event logs. For example, your application might require multiple sources configured for different event logs or different resource files.

If you change the Source value, the EventLog to which it is registered is closed and all event handles are released.

The source must be configured either for writing localized entries or for writing direct strings. If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

To change the configuration details of an existing source, you must delete the source and then create it with the new configuration. If other applications or components use the existing source, create a new source with the updated configuration rather than deleting the existing source.

NoteNote:

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

TopicLocation
How to: Build and Run the Profile Provider ExampleBuilding ASP .NET Web Applications
How to: Build and Run the Profile Provider ExampleBuilding ASP .NET Web Applications

The following example creates the source MySource if it does not already exist, and writes an entry to the event log MyNewLog.

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"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }
                
        // 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.");                                                                        
    }
}
   

import System.*;
import System.Diagnostics.*;
import System.Threading.*;

class MySample
{
    public static void main(String[] args)
    {
        // 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.set_Source("MySource");

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
        Console.WriteLine("Message written to event log.");
    } //main
} //MySample

Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft