EventSourceCreationData 类

定义

表示用于在本地或远程计算机上创建事件日志源的配置设置。

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

示例

下面的代码示例通过命令行参数设置事件源的配置属性。 输入参数指定事件源名称、事件日志名称、计算机名称和事件消息资源文件。 该代码示例验证源是否与现有事件源不冲突,然后为指定的事件日志创建新的事件源。

#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使用 类配置新的源,以便将本地化条目写入事件日志。 无需使用此类从事件日志中读取数据。

此类定义新事件源及其关联事件日志的配置设置。 关联的事件日志可以位于本地计算机或远程计算机上。 若要在本地计算机上为新的或现有的事件日志创建新的源,请设置 LogNameEventSourceCreationDataSource 属性并调用 EventLog.CreateEventSource(EventSourceCreationData) 方法。 此方法创建在 属性中指定的 Source 事件源,并为 中指定的 LogName事件日志注册它。 此行为类似于使用 EventLogInstaller 类为事件日志注册事件源。

WriteEvent使用 和 WriteEntry 方法将事件写入事件日志。 必须指定事件源才能写入事件;在使用源写入第一个条目之前,必须创建并配置事件源。

在应用程序安装期间创建新的事件源。 这允许操作系统有时间刷新其已注册事件源及其配置的列表。 如果操作系统尚未刷新其事件源列表,并且您尝试使用新源编写事件,则写入操作将失败。 可以使用 或使用 CreateEventSource 方法配置新源EventLogInstaller。 必须在计算机上具有管理权限才能创建新的事件源。

可以为现有事件日志或新的事件日志创建事件源。 为新事件日志创建新源时,系统会为该日志注册源,但在写入第一个条目之前不会创建该日志。

每个源一次只能写入一个事件日志;但是,应用程序可以使用多个源写入多个事件日志。 例如,应用程序可能需要为不同的事件日志或不同的资源文件配置多个源。

若要更改现有源的配置详细信息,必须删除该源,然后使用新配置创建它。 如果其他应用程序或组件使用现有源,请使用更新的配置创建新的源,而不是删除现有源。

可以使用事件类别和消息字符串的本地化资源注册事件源。 应用程序可以使用资源标识符写入事件日志条目,而不是指定实际字符串。 事件查看器使用资源标识符根据当前语言设置查找并显示本地化资源文件中的相应字符串。 可以为事件类别、消息和参数插入字符串注册单独的文件,也可以为所有三种类型的字符串注册相同的资源文件。 CategoryCount使用 、CategoryResourceFileMessageResourceFileParameterResourceFile 属性将源配置为将本地化条目写入事件日志。 如果应用程序将字符串值直接写入事件日志,则无需设置这些属性。

必须为编写本地化条目或编写直接字符串配置源。 方法 WriteEntry 将给定的字符串直接写入事件日志;它不使用可本地化的消息资源文件。 WriteEvent使用 方法通过本地化的消息资源文件写入事件。

如果应用程序使用资源标识符和字符串值写入条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后在 方法中 WriteEvent 使用该源将使用资源标识符的条目写入事件日志。 然后创建一个不带资源文件的其他源,并在 方法中 WriteEntry 使用该源将字符串直接写入使用该源的事件日志。

构造函数

EventSourceCreationData(String, String)

使用指定的事件源和事件日志名称对 EventSourceCreationData 类的新实例进行初始化。

属性

CategoryCount

获取或设置类别资源文件中类别的数目。

CategoryResourceFile

获取或设置包含源的类别字符串的资源文件的路径。

LogName

获取或设置事件日志的名称,事件源要向该日志写入项。

MachineName

获取或设置在其上注册事件源的计算机的名称。

MessageResourceFile

获取或设置消息资源文件的路径,该文件包含源的消息格式字符串。

ParameterResourceFile

获取或设置资源文件的路径,该文件包含源的消息参数字符串。

Source

获取或设置要在事件日志中注册为事件源的名称。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅