EventSourceCreationData 类

定义

表示用于在本地或远程计算机上创建事件日志源的配置设置。Represents the configuration settings used to create an event log source on the local computer or a remote computer.

public ref class EventSourceCreationData
public class EventSourceCreationData
type EventSourceCreationData = class
Public Class EventSourceCreationData
继承
EventSourceCreationData

示例

下面的代码示例通过命令行参数设置事件源的配置属性。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;

[STAThread]
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 ];
      }
   }
   else
   {
      
      // 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;
            }
         }
         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;
         }
      }
   }
   else
   {
      
      // 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 );
      }
      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 );
      }
   }

   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!" );
   }
}

using System;
using System.Globalization;
using System.Diagnostics;

namespace EventLogSamples
{
    class CreateSourceSample
    {
        [STAThread]
        static void Main(string[] args)
        {
            EventSourceCreationData mySourceData = new EventSourceCreationData("", "");
            bool registerSource = 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];
                }

                if (args.Length > 2)
                {
                    mySourceData.MachineName = args[2];
                }
                if ((args.Length > 3) && (args[3].Length > 0))
                {
                    mySourceData.MessageResourceFile = args[3];
                }
            }
            else
            {
                // 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 != null) &&
                    (mySourceData.MessageResourceFile.Length > 0))
                {
                    if (mySourceData.MachineName == ".")
                    {
                        if (!System.IO.File.Exists(mySourceData.MessageResourceFile))
                        {
                            Console.WriteLine("File {0} not found - message file not set for source.",
                                mySourceData.MessageResourceFile);
                            registerSource = false;
                        }
                    }
                    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;
                    }
                }
            }
            else
            {
                // 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);
                }
                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);
                }
            }

            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!");
            }
        }
    }
}
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
    End Class
End Namespace 'EventLogSamples

注解

使用 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. 若要为本地计算机上的新的或现有的事件日志创建新的源,请设置的 LogNameSource 属性, EventSourceCreationData 并调用 EventLog.CreateEventSource(EventSourceCreationData) 方法。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. 此方法将创建在属性中指定的事件源 Source ,并将其注册为中指定的事件日志 LogNameThis method creates the event source you specify in the Source property and registers it for the event log specified in LogName. 此行为类似于使用 EventLogInstaller 类注册事件日志的事件源。This behavior is similar to using the EventLogInstaller class to register an event source for an event log.

使用 WriteEventWriteEntry 方法可将事件写入事件日志。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. 您可以使用或方法来配置新源 EventLogInstaller CreateEventSourceYou 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. 使用 CategoryCountCategoryResourceFileMessageResourceFile 和属性将 ParameterResourceFile 源配置为将本地化项写入事件日志。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. WriteEntry方法将给定字符串直接写入事件日志; 它不使用可本地化的消息资源文件。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. 使用 WriteEvent 方法可使用本地化消息资源文件编写事件。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. 例如,使用资源文件配置一个源,然后在方法中使用该源,将 WriteEvent 使用资源标识符的项写入事件日志。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. 然后,创建一个没有资源文件的不同源,并在方法中使用该源,使用 WriteEntry 该源直接向事件日志写入字符串。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.

构造函数

EventSourceCreationData(String, String)

使用指定的事件源和事件日志名称对 EventSourceCreationData 类的新实例进行初始化。Initializes a new instance of the EventSourceCreationData class with a specified event source and event log name.

属性

CategoryCount

获取或设置类别资源文件中类别的数目。Gets or sets the number of categories in the category resource file.

CategoryResourceFile

获取或设置包含源的类别字符串的资源文件的路径。Gets or sets the path of the resource file that contains category strings for the source.

LogName

获取或设置事件日志的名称,事件源要向该日志写入项。Gets or sets the name of the event log to which the source writes entries.

MachineName

获取或设置在其上注册事件源的计算机的名称。Gets or sets the name of the computer on which to register the event source.

MessageResourceFile

获取或设置消息资源文件的路径,该文件包含源的消息格式字符串。Gets or sets the path of the message resource file that contains message formatting strings for the source.

ParameterResourceFile

获取或设置资源文件的路径,该文件包含源的消息参数字符串。Gets or sets the path of the resource file that contains message parameter strings for the source.

Source

获取或设置要在事件日志中注册为事件源的名称。Gets or sets the name to register with the event log as an event source.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅