This documentation is archived and is not being maintained.

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 Class EventSourceCreationData
Dim instance As EventSourceCreationData

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 

                ' Display a syntax help message.
                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, _
                ' 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.", _
                            registerSource = False 
                        End If 
                        ' 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 
                ' 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, _

                ' 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)

                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)
                Console.WriteLine("Event source was registered successfully!")
            End If 

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


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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0