EventSourceCreationData Class

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

System.Object
  System.Diagnostics.EventSourceCreationData

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

'Declaration
Public Class EventSourceCreationData

The EventSourceCreationData type exposes the following members.

  NameDescription
Public methodEventSourceCreationDataInitializes a new instance of the EventSourceCreationData class with a specified event source and event log name.
Top

  NameDescription
Public propertyCategoryCountGets or sets the number of categories in the category resource file.
Public propertyCategoryResourceFileGets or sets the path of the resource file that contains category strings for the source.
Public propertyLogNameGets or sets the name of the event log to which the source writes entries.
Public propertyMachineNameGets or sets the name of the computer on which to register the event source.
Public propertyMessageResourceFileGets or sets the path of the message resource file that contains message formatting strings for the source.
Public propertyParameterResourceFileGets or sets the path of the resource file that contains message parameter strings for the source.
Public propertySourceGets or sets the name to register with the event log as an event source.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

Windows 98 Platform Note: 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.

Imports System
Imports System.Globalization
Imports System.Diagnostics

Namespace EventLogSamples
    Class CreateSourceSample

        Public Shared Sub Main(ByVal args() As String)

            Dim mySourceData As EventSourceCreationData = new EventSourceCreationData("", "")
            Dim registerSource As Boolean = True 

            ' Process input parameters. 
            If args.Length > 0
                ' Require at least the source name.

                mySourceData.Source = args(0)

                If args.Length > 1

                    mySourceData.LogName = args(1)

                End If 
                If args.Length > 2

                    mySourceData.MachineName = args(2)

                End If 
                If args.Length > 3 AndAlso args(3).Length > 0

                    mySourceData.MessageResourceFile = args(3)

                End If 

            Else  
                ' Display a syntax help message.
                Console.WriteLine("Input:")
                Console.WriteLine(" source [event log] [machine name] [resource file]")

                registerSource = False 
            End If 

            ' Set defaults for parameters missing input. 
            If mySourceData.MachineName.Length = 0

                ' Default to the local computer.
                mySourceData.MachineName = "." 
            End If 
            If mySourceData.LogName.Length = 0
                ' Default to the Application log.
                mySourceData.LogName = "Application" 
            End If 

            ' Determine if the source exists on the specified computer. 
            If Not 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 <> Nothing AndAlso _
                   mySourceData.MessageResourceFile.Length > 0

                    If mySourceData.MachineName = "." 
                        If Not System.IO.File.Exists(mySourceData.MessageResourceFile)

                            Console.WriteLine("File {0} not found - message file not set for source.", _
                                mySourceData.MessageResourceFile)
                            registerSource = False 
                        End If 
                    Else 
                        ' 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 

                    End If 
                End If 
            Else  
                ' Do not register the source, it already exists.
                registerSource = False 

                ' Get the event log corresponding to the existing source. 
                Dim sourceLog As string
                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)
                Else  
                    ' 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)

                End If 
            End If 

            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!")
            End If 

        End Sub 'Main
    End Class 'CreateSourceSample
End Namespace 'EventLogSamples

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft