EventSourceCreationData Class


Represents the configuration settings used to create an event log source on the local computer or a remote computer.

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


public ref class EventSourceCreationData 

System_CAPS_pubmethodEventSourceCreationData(String^, String^)

Initializes a new instance of the EventSourceCreationData class with a specified event source and event log name.


Gets or sets the number of categories in the category resource file.


Gets or sets the path of the resource file that contains category strings for the source.


Gets or sets the name of the event log to which the source writes entries.


Gets or sets the name of the computer on which to register the event source.


Gets or sets the path of the message resource file that contains message formatting strings for the source.


Gets or sets the path of the resource file that contains message parameter strings for the source.


Gets or sets the name to register with the event log as an event source.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

Use the EventSourceCreationData class to configure a new source for writing localized entries to an event log. It is not necessary to use this class to read from an event log.

This class defines the configuration settings for a new event source and its associated event log. The associated event log can be on the local computer or a remote computer. To create a new source for a new or existing event log on the local computer, set the LogName and Source properties of an EventSourceCreationData and call the EventLog::CreateEventSource(EventSourceCreationData^) method. This method creates the event source you specify in the Source property and registers it for the event log specified in LogName. This behavior is similar to using the EventLogInstaller class to register an event source for an event log.

Use the WriteEvent and WriteEntry methods 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 configurations. 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.

Each source can only write to one event log at a time; however, your application can use multiple sources to write to multiple event logs. For example, your application might need multiple sources configured for different event logs or different resource files.

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.

You can register the event source with localized resources for your event category and message strings. Your application can write event log entries using resource identifiers, rather than specifying the actual string. The Event Viewer uses the resource identifier to find and display the corresponding string from the localized resource file based on current language settings. You can register a separate file for event categories, messages, and parameter insertion strings, or you can register the same resource file for all three types of strings. Use the CategoryCount, CategoryResourceFile, MessageResourceFile, and ParameterResourceFile properties to configure the source to write localized entries to the event log. If your application writes string values directly to the event log, you do not need to set these properties.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

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.


Event logs are not supported on Windows 98/Windows Millennium Edition (Me).

The following code example sets the configuration properties for an event source from command-line arguments. The input arguments specify the event source name, event log name, computer name, and event message resource file. The code example verifies that the source does not conflict with an existing event source, and then creates the new event source for the specified event log.

#using <System.dll>

using namespace System;
using namespace System::Globalization;
using namespace System::Diagnostics;

int main()
   array<String^>^args = Environment::GetCommandLineArgs();

   EventSourceCreationData ^ mySourceData = gcnew EventSourceCreationData( "","" );
   bool registerSource = true;

   // Process input parameters.
   if ( args->Length > 1 )

      // Require at least the source name.
      mySourceData->Source = args[ 1 ];
      if ( args->Length > 2 )
         mySourceData->LogName = args[ 2 ];

      if ( args->Length > 3 )
         mySourceData->MachineName = args[ 3 ];

      if ( (args->Length > 4) && (args[ 4 ]->Length > 0) )
         mySourceData->MessageResourceFile = args[ 4 ];

      // Display a syntax help message.
      Console::WriteLine( "Input:" );
      Console::WriteLine( " source [event log] [machine name] [resource file]" );
      registerSource = false;

   // Set defaults for parameters missing input.
   if ( mySourceData->MachineName->Length == 0 )

      // Default to the local computer.
      mySourceData->MachineName = ".";

   if ( mySourceData->LogName->Length == 0 )

      // Default to the Application log.
      mySourceData->LogName = "Application";

   // Determine if the source exists on the specified computer.
   if (  !EventLog::SourceExists( mySourceData->Source, mySourceData->MachineName ) )

      // The source does not exist.  
      // Verify that the message file exists
      // and the event log is local.
      if ( (mySourceData->MessageResourceFile != nullptr) && (mySourceData->MessageResourceFile->Length > 0) )
         if ( mySourceData->MachineName->Equals( "." ) )
            if (  !System::IO::File::Exists( mySourceData->MessageResourceFile ) )
               Console::WriteLine( "File {0} not found - message file not set for source.", mySourceData->MessageResourceFile );
               registerSource = false;

            // For simplicity, do not allow setting the message
            // file for a remote event log.  To set the message
            // for a remote event log, and for source registration,
            // the file path should be specified with system-wide
            // environment variables that are valid on the remote
            // computer, such as
            // "%SystemRoot%\system32\myresource.dll".
            Console::WriteLine( "Message resource file ignored for remote event log." );
            registerSource = false;

      // Do not register the source, it already exists.
      registerSource = false;

      // Get the event log corresponding to the existing source.
      String^ sourceLog;
      sourceLog = EventLog::LogNameFromSourceName( mySourceData->Source, mySourceData->MachineName );

      // Determine if the event source is registered for the 
      // specified log.
      if ( sourceLog->ToUpper( CultureInfo::InvariantCulture ) != mySourceData->LogName->ToUpper( CultureInfo::InvariantCulture ) )

         // An existing source is registered 
         // to write to a different event log.
         Console::WriteLine( "Warning: source {0} is already registered to write to event log {1}", mySourceData->Source, sourceLog );

         // The source is already registered 
         // to write to the specified event log.
         Console::WriteLine( "Source {0} already registered to write to event log {1}", mySourceData->Source, sourceLog );

   if ( registerSource )

      // Register the new event source for the specified event log.
      Console::WriteLine( "Registering new source {0} for event log {1}.", mySourceData->Source, mySourceData->LogName );
      EventLog::CreateEventSource( mySourceData );
      Console::WriteLine( "Event source was registered successfully!" );


for calling any member of EventSourceCreationData with full trust. Associated enumeration: PermissionState::Unrestricted

.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top