EventLog.CreateEventSource 方法

定义

建立一个能够将事件信息写入到系统的特定日志中的应用程序。

重载

CreateEventSource(EventSourceCreationData)

通过使用为该事件源和对应的事件日志指定的配置属性,建立一个写入本地化事件消息的有效事件源。

CreateEventSource(String, String)

建立指定的源名称作为向本地计算机上的日志中写入日志项的有效事件源。 此方法还可在本地计算机上创建一个新的自定义日志。

CreateEventSource(String, String, String)
已过时.
已过时.
已过时.

建立指定的源名称作为向指定计算机上的日志中写入项的有效事件源。 此方法还可用于在指定计算机上创建一个新的自定义日志。

CreateEventSource(EventSourceCreationData)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

通过使用为该事件源和对应的事件日志指定的配置属性,建立一个写入本地化事件消息的有效事件源。

public:
 static void CreateEventSource(System::Diagnostics::EventSourceCreationData ^ sourceData);
public static void CreateEventSource (System.Diagnostics.EventSourceCreationData sourceData);
static member CreateEventSource : System.Diagnostics.EventSourceCreationData -> unit
Public Shared Sub CreateEventSource (sourceData As EventSourceCreationData)

参数

sourceData
EventSourceCreationData

事件源及其目标事件日志的配置属性。

例外

sourceData 中指定的计算机名称无效。

- 或 -

sourceData 中指定的源名称为 null

- 或 -

sourceData 中指定的日志名称无效。 事件日志名称必须由可打印字符组成,不能包含字符“*”、“?”或“\”。

- 或 -

sourceData 中指定的日志名称对用户日志创建无效。 事件日志名称 AppEvent、SysEvent 和 SecEvent 是保留为系统使用的。

- 或 -

该日志名称与一个现有事件源名称相匹配。

- 或 -

sourceData 中指定的源名称导致注册表项路径的长度超过 254 个字符。

- 或 -

sourceData 中指定的日志名称的前 8 个字符不唯一。

- 或 -

sourceData 中指定的源名称已经注册过。

- 或 -

sourceData 中指定的源名称与一个现有事件日志名称相匹配。

事件日志的注册表项未能打开。

sourceDatanull

示例

以下示例确定是否在本地计算机上注册名为 SampleApplicationSource 的事件源。 如果事件源不存在,则本示例将设置源的消息资源文件并创建新的事件源。 最后,该示例使用 中的资源标识符值和 中的 DisplayNameMsgId 资源文件路径 messageFile设置事件日志的本地化显示名称。

void CreateEventSourceSample1( String^ messageFile )
{
   String^ myLogName;
   String^ sourceName = "SampleApplicationSource";
   
   // Create the event source if it does not exist.
   if (  !EventLog::SourceExists( sourceName ) )
   {
      
      // Create a new event source for the custom event log
      // named "myNewLog."  
      myLogName = "myNewLog";
      EventSourceCreationData ^ mySourceData = gcnew EventSourceCreationData( sourceName,myLogName );
      
      // Set the message resource file that the event source references.
      // All event resource identifiers correspond to text in this file.
      if (  !System::IO::File::Exists( messageFile ) )
      {
         Console::WriteLine( "Input message resource file does not exist - {0}", messageFile );
         messageFile = "";
      }
      else
      {
         
         // Set the specified file as the resource
         // file for message text, category text, and 
         // message parameter strings.  
         mySourceData->MessageResourceFile = messageFile;
         mySourceData->CategoryResourceFile = messageFile;
         mySourceData->CategoryCount = CategoryCount;
         mySourceData->ParameterResourceFile = messageFile;
         Console::WriteLine( "Event source message resource file set to {0}", messageFile );
      }

      Console::WriteLine( "Registering new source for event log." );
      EventLog::CreateEventSource( mySourceData );
   }
   else
   {
      
      // Get the event log corresponding to the existing source.
      myLogName = EventLog::LogNameFromSourceName( sourceName, "." );
   }

   
   // Register the localized name of the event log.
   // For example, the actual name of the event log is "myNewLog," but
   // the event log name displayed in the Event Viewer might be
   // "Sample Application Log" or some other application-specific
   // text.
   EventLog^ myEventLog = gcnew EventLog( myLogName,".",sourceName );
   if ( messageFile->Length > 0 )
   {
      myEventLog->RegisterDisplayName( messageFile, DisplayNameMsgId );
   }   
}
static void CreateEventSourceSample1(string messageFile)
{
    string myLogName;
    string sourceName = "SampleApplicationSource";

    // Create the event source if it does not exist.
    if(!EventLog.SourceExists(sourceName))
    {
        // Create a new event source for the custom event log
        // named "myNewLog."

        myLogName = "myNewLog";
        EventSourceCreationData mySourceData = new EventSourceCreationData(sourceName, myLogName);

        // Set the message resource file that the event source references.
        // All event resource identifiers correspond to text in this file.
        if (!System.IO.File.Exists(messageFile))
        {
            Console.WriteLine("Input message resource file does not exist - {0}",
                messageFile);
            messageFile = "";
        }
        else
        {
            // Set the specified file as the resource
            // file for message text, category text, and
            // message parameter strings.

            mySourceData.MessageResourceFile = messageFile;
            mySourceData.CategoryResourceFile = messageFile;
            mySourceData.CategoryCount = CategoryCount;
            mySourceData.ParameterResourceFile = messageFile;

            Console.WriteLine("Event source message resource file set to {0}",
                messageFile);
        }

        Console.WriteLine("Registering new source for event log.");
        EventLog.CreateEventSource(mySourceData);
    }
    else
    {
        // Get the event log corresponding to the existing source.
        myLogName = EventLog.LogNameFromSourceName(sourceName,".");
    }

    // Register the localized name of the event log.
    // For example, the actual name of the event log is "myNewLog," but
    // the event log name displayed in the Event Viewer might be
    // "Sample Application Log" or some other application-specific
    // text.
    EventLog myEventLog = new EventLog(myLogName, ".", sourceName);

    if (messageFile.Length > 0)
    {
        myEventLog.RegisterDisplayName(messageFile, DisplayNameMsgId);
    }
}
Public Shared Sub CreateEventSourceSample1(ByVal messageFile As String)

    Dim myLogName As String
    Dim sourceName As String = "SampleApplicationSource"

    ' Create the event source if it does not exist.
    If Not EventLog.SourceExists(sourceName)
    
        ' Create a new event source for the custom event log
        ' named "myNewLog."  

        myLogName = "myNewLog"
        Dim mySourceData As EventSourceCreationData = New EventSourceCreationData(sourceName, myLogName)

        ' Set the message resource file that the event source references.
        ' All event resource identifiers correspond to text in this file.
        If Not System.IO.File.Exists(messageFile)

            Console.WriteLine("Input message resource file does not exist - {0}", _
                messageFile)
            messageFile = ""
        Else 
            ' Set the specified file as the resource
            ' file for message text, category text and 
            ' message parameters strings.

            mySourceData.MessageResourceFile = messageFile
            mySourceData.CategoryResourceFile = messageFile
            mySourceData.CategoryCount = CategoryCount
            mySourceData.ParameterResourceFile = messageFile

            Console.WriteLine("Event source message resource file set to {0}", _
                messageFile)
        End If

        Console.WriteLine("Registering new source for event log.")
        EventLog.CreateEventSource(mySourceData)
    Else
        ' Get the event log corresponding to the existing source.
        myLogName = EventLog.LogNameFromSourceName(sourceName,".")
    End If

    ' Register the localized name of the event log.
    ' For example, the actual name of the event log is "myNewLog," but
    ' the event log name displayed in the Event Viewer might be
    ' "Sample Application Log" or some other application-specific
    ' text.
    Dim myEventLog As EventLog = New EventLog(myLogName, ".", sourceName)
    
    If messageFile.Length > 0
        myEventLog.RegisterDisplayName(messageFile, DisplayNameMsgId)
    End If
End Sub

该示例使用以下消息文本文件,该文件内置于资源库中 EventLogMsgs.dll。 消息文本文件是创建消息资源文件的源。 消息文本文件定义类别、事件消息和参数插入字符串的资源标识符和文本。 具体而言,资源标识符 5001 是为事件日志的本地化名称定义的。

; // EventLogMsgs.mc  
; // ********************************************************  

; // Use the following commands to build this file:  

; //   mc -s EventLogMsgs.mc  
; //   rc EventLogMsgs.rc  
; //   link /DLL /SUBSYSTEM:WINDOWS /NOENTRY /MACHINE:x86 EventLogMsgs.Res   
; // ********************************************************  

; // - Event categories -  
; // Categories must be numbered consecutively starting at 1.  
; // ********************************************************  

MessageId=0x1  
Severity=Success  
SymbolicName=INSTALL_CATEGORY  
Language=English  
Installation  
.  

MessageId=0x2  
Severity=Success  
SymbolicName=QUERY_CATEGORY  
Language=English  
Database Query  
.  

MessageId=0x3  
Severity=Success  
SymbolicName=REFRESH_CATEGORY  
Language=English  
Data Refresh  
.  

; // - Event messages -  
; // *********************************  

MessageId = 1000  
Severity = Success  
Facility = Application  
SymbolicName = AUDIT_SUCCESS_MESSAGE_ID_1000  
Language=English  
My application message text, in English, for message id 1000, called from %1.  
.  

MessageId = 1001  
Severity = Warning  
Facility = Application  
SymbolicName = AUDIT_FAILED_MESSAGE_ID_1001  
Language=English  
My application message text, in English, for message id 1001, called from %1.  
.  

MessageId = 1002  
Severity = Success  
Facility = Application  
SymbolicName = GENERIC_INFO_MESSAGE_ID_1002  
Language=English  
My generic information message in English, for message id 1002.  
.  

MessageId = 1003  
Severity = Warning  
Facility = Application  
SymbolicName = GENERIC_WARNING_MESSAGE_ID_1003  
Language=English  
My generic warning message in English, for message id 1003, called from %1.  
.  

MessageId = 1004  
Severity = Success  
Facility = Application  
SymbolicName = UPDATE_CYCLE_COMPLETE_MESSAGE_ID_1004  
Language=English  
The update cycle is complete for %%5002.  
.  

MessageId = 1005  
Severity = Warning  
Facility = Application  
SymbolicName = SERVER_CONNECTION_DOWN_MESSAGE_ID_1005  
Language=English  
The refresh operation did not complete because the connection to server %1 could not be established.  
.  

; // - Event log display name -  
; // ********************************************************  

MessageId = 5001  
Severity = Success  
Facility = Application  
SymbolicName = EVENT_LOG_DISPLAY_NAME_MSGID  
Language=English  
Sample Event Log  
.  

; // - Event message parameters -  
; //   Language independent insertion strings  
; // ********************************************************  

MessageId = 5002  
Severity = Success  
Facility = Application  
SymbolicName = EVENT_LOG_SERVICE_NAME_MSGID  
Language=English  
SVC_UPDATE.EXE  
.  

注解

使用此重载配置新源,以便将条目写入本地计算机或远程计算机上的事件日志。 无需使用此方法从事件日志中读取数据。

方法 CreateEventSource 使用输入 sourceDataSourceLogNameMachineName 属性在目标计算机上为新源及其关联的事件日志创建注册表值。 新的源名称不能与目标计算机上的现有源名称或现有事件日志名称匹配。 LogName如果未设置 属性,则会为应用程序事件日志注册源。 MachineName如果未设置 ,则在本地计算机上注册源。

注意

若要在 Windows Vista 及更高版本或 Windows Server 2003 中创建事件源,必须具有管理权限。

此要求的原因是必须搜索所有事件日志(包括安全性),以确定事件源是否唯一。 从 Windows Vista 开始,用户无权访问安全日志;因此, SecurityException 会引发 。

从 Windows Vista 开始,用户帐户控制 (UAC) 确定用户的权限。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问安全日志的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

使用 WriteEventWriteEntry 将事件写入事件日志。 必须指定事件源才能写入事件;必须先创建并配置事件源,然后才能使用源编写第一个条目。

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

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

操作系统将事件日志存储为文件。 使用 EventLogInstallerCreateEventSource 创建新的事件日志时,关联的文件将存储在指定计算机上的 %SystemRoot%\System32\Config 目录中。 通过将 属性的前 8 个字符 Log 追加到“.evt”文件扩展名来设置文件名。

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

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

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

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

注意

如果为事件日志配置了源,而你将其重新配置为另一个事件日志,则必须重新启动计算机,更改才能生效。

另请参阅

适用于

CreateEventSource(String, String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

建立指定的源名称作为向本地计算机上的日志中写入日志项的有效事件源。 此方法还可在本地计算机上创建一个新的自定义日志。

public:
 static void CreateEventSource(System::String ^ source, System::String ^ logName);
public static void CreateEventSource (string source, string logName);
static member CreateEventSource : string * string -> unit
Public Shared Sub CreateEventSource (source As String, logName As String)

参数

source
String

应用程序在本地计算机上注册时所采用的源名称。

logName
String

源的项写入的日志名。 可能的值包括“应用程序”、“系统”或自定义事件日志。

例外

source 为空字符串 ("") 或 null

- 或 -

logName 不是有效的事件日志名称。 事件日志名称必须由可打印字符组成,不能包含字符“*”、“?”或“\”。

- 或 -

logName对用户日志创建无效。 事件日志名称 AppEvent、SysEvent 和 SecEvent 是保留为系统使用的。

- 或 -

该日志名称与一个现有事件源名称相匹配。

- 或 -

此源名称导致注册表项路径的长度超过 254 个字符。

- 或 -

logName 的前 8 个字符与现有事件日志名称的前 8 个字符相匹配。

- 或 -

无法注册该源,因为它已存在于本地计算机上。

- 或 -

该源名称与一个现有事件日志名称相匹配。

事件日志的注册表项未能在本地计算机上打开。

示例

以下示例创建源 MySource (如果尚不存在),并将条目写入事件日志 MyNewLog

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create the source, if it does not already exist.
   if (  !EventLog::SourceExists( "MySource" ) )
   {
      //An event log source should not be created and immediately used.
      //There is a latency time to enable the source, it should be created
      //prior to executing the application that uses the source.
      //Execute this sample a second time to use the new source.
      EventLog::CreateEventSource( "MySource", "MyNewLog" );
      Console::WriteLine( "CreatingEventSource" );
      // The source is created.  Exit the application to allow it to be registered.
      return 0;
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog;
   myLog->Source = "MySource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing to event log." );
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource"))
        {
             //An event log source should not be created and immediately used.
             //There is a latency time to enable the source, it should be created
             //prior to executing the application that uses the source.
             //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.
        myLog.WriteEntry("Writing to event log.");
    }
}
Option Explicit
Option Strict

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        
        If Not EventLog.SourceExists("MySource") Then
            ' Create the source, if it does not already exist.
            ' An event log source should not be created and immediately used.
            ' There is a latency time to enable the source, it should be created
            ' prior to executing the application that uses the source.
            ' Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
            'The source is created.  Exit the application to allow it to be registered.
            Return
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
    End Sub
End Class

注解

使用此重载可以创建自定义日志,或者创建 并注册 Source 到本地计算机上的现有日志。

如果在logName调用 CreateEventSource时为 null 或空字符串 (“”) ,则日志默认为应用程序日志。 如果本地计算机上不存在该日志,系统会创建自定义日志,并将应用程序注册为 Source 该日志的 。

注意

若要在 Windows Vista 及更高版本或 Windows Server 2003 中创建事件源,必须具有管理权限。

此要求的原因是必须搜索所有事件日志(包括安全性),以确定事件源是否唯一。 从 Windows Vista 开始,用户无权访问安全日志;因此, SecurityException 会引发 。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问安全日志的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

仅当正在写入事件日志时,才需要创建事件源。 在将条目写入事件日志之前,必须将事件源注册到事件日志中,作为有效的事件源。 编写日志条目时,系统会使用 Source 查找要在其中放置条目的相应日志。 如果要读取事件日志,可以指定 Source或 和 LogMachineName

注意

如果要连接到本地计算机上的日志, MachineName 则无需指定 。 如果在从日志读取时未指定 MachineName ,则本地计算机 (“。”假定 ) 。

使用 WriteEventWriteEntry 将事件写入事件日志。 必须指定事件源才能写入事件;必须先创建并配置事件源,然后才能使用源编写第一个条目。

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

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

操作系统将事件日志存储为文件。 使用 EventLogInstallerCreateEventSource 创建新的事件日志时,关联的文件将存储在指定计算机上的 %SystemRoot%\System32\Config 目录中。 通过将 属性的前 8 个字符 Log 追加到“.evt”文件扩展名来设置文件名。

源在本地计算机上必须是唯一的;新的源名称不能与现有源名称或现有事件日志名称匹配。 每个源一次只能写入一个事件日志;但是,应用程序可以使用多个源写入多个事件日志。 例如,应用程序可能需要为不同的事件日志或不同的资源文件配置多个源。

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

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

注意

如果源已映射到日志,并且你将其重新映射到新日志,则必须重新启动计算机才能使更改生效。

另请参阅

适用于

CreateEventSource(String, String, String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

注意

This method has been deprecated. Please use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead. http://go.microsoft.com/fwlink/?linkid=14202

注意

This method has been deprecated. Please use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead. https://go.microsoft.com/fwlink/?linkid=14202

注意

EventLog.CreateEventSource has been deprecated. Use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.

建立指定的源名称作为向指定计算机上的日志中写入项的有效事件源。 此方法还可用于在指定计算机上创建一个新的自定义日志。

public:
 static void CreateEventSource(System::String ^ source, System::String ^ logName, System::String ^ machineName);
[System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
public static void CreateEventSource (string source, string logName, string machineName);
[System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.  https://go.microsoft.com/fwlink/?linkid=14202")]
public static void CreateEventSource (string source, string logName, string machineName);
[System.Obsolete("EventLog.CreateEventSource has been deprecated. Use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.")]
public static void CreateEventSource (string source, string logName, string machineName);
public static void CreateEventSource (string source, string logName, string machineName);
[<System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.  http://go.microsoft.com/fwlink/?linkid=14202")>]
static member CreateEventSource : string * string * string -> unit
[<System.Obsolete("This method has been deprecated.  Please use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.  https://go.microsoft.com/fwlink/?linkid=14202")>]
static member CreateEventSource : string * string * string -> unit
[<System.Obsolete("EventLog.CreateEventSource has been deprecated. Use System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData) instead.")>]
static member CreateEventSource : string * string * string -> unit
static member CreateEventSource : string * string * string -> unit
Public Shared Sub CreateEventSource (source As String, logName As String, machineName As String)

参数

source
String

应用程序在指定计算机上注册时所采用的源。

logName
String

源的项写入的日志名。 可能的值包括“应用程序”、“系统”或自定义事件日志。 如果不指定值,则 logName 默认为应用程序。

machineName
String

用来注册此事件源的计算机名称,对于本地计算机则为“.”。

属性

例外

machineName 不是有效的计算机名称。

- 或 -

source 为空字符串 ("") 或 null

- 或 -

logName 不是有效的事件日志名称。 事件日志名称必须由可打印字符组成,不能包含字符“*”、“?”或“\”。

- 或 -

logName对用户日志创建无效。 事件日志名称 AppEvent、SysEvent 和 SecEvent 是保留为系统使用的。

- 或 -

该日志名称与一个现有事件源名称相匹配。

- 或 -

此源名称导致注册表项路径的长度超过 254 个字符。

- 或 -

logName 的前 8 个字符与指定计算机上现有事件日志名称的前 8 个字符相匹配。

- 或 -

源无法注册,因为它已存在于指定的计算机上。

- 或 -

该源名称与一个现有事件源名称相匹配。

事件日志的注册表项未能在指定的计算机上打开。

示例

以下示例在计算机上创建源MySource,并将一个条目写入事件日志 MyNewLogMyServer

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create the source, if it does not already exist.
   if (  !EventLog::SourceExists( "MySource", "MyServer" ) )
   {
      EventLog::CreateEventSource( "MySource", "MyNewLog", "MyServer" );
      Console::WriteLine( "CreatingEventSource" );
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog;
   myLog->Source = "MySource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing to event log." );
   Console::WriteLine( "Message written to event log." );
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource", "MyServer"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.
        myLog.WriteEntry("Writing to event log.");

        Console.WriteLine("Message written to event log.");
    }
}
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create the source, if it does not already exist.
        If Not EventLog.SourceExists("MySource", "MyServer") Then
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer")
            Console.WriteLine("CreatingEventSource")
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
        
        Console.WriteLine("Message written to event log.")
    End Sub
End Class

注解

使用此重载可以创建自定义日志,或者创建 并注册 Source 到指定计算机上的现有日志。

如果在logName调用 CreateEventSource时为 null 或空字符串 (“”) ,则日志默认为应用程序日志。 如果指定计算机上不存在该日志,系统会创建自定义日志,并将应用程序注册为 Source 该日志的 。

仅当正在写入事件日志时,才需要创建事件源。 在将条目写入事件日志之前,必须将事件源注册到事件日志中,作为有效的事件源。 编写日志条目时,系统会使用 Source 查找要在其中放置条目的相应日志。 如果要读取事件日志,可以指定 Source或 和 LogMachineName

注意

若要在 Windows Vista 及更高版本或 Windows Server 2003 中创建事件源,必须具有管理权限。

此要求的原因是必须搜索所有事件日志(包括安全性),以确定事件源是否唯一。 在 Windows Vista 及更高版本中,用户没有访问安全日志的权限;因此, SecurityException 会引发 。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问安全日志的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

使用 WriteEventWriteEntry 将事件写入事件日志。 必须指定事件源才能写入事件;必须先创建并配置事件源,然后才能使用源编写第一个条目。

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

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

操作系统将事件日志存储为文件。 使用 EventLogInstallerCreateEventSource 创建新的事件日志时,关联的文件存储在指定计算机上的 %SystemRoot%\System32\Config 目录中。 通过将属性的前 8 个字符 Log 追加“.evt”文件扩展名来设置文件名。

源在本地计算机上必须是唯一的;新的源名称不能与现有源名称或现有事件日志名称匹配。 每个源一次只能写入一个事件日志;但是,应用程序可以使用多个源写入多个事件日志。 例如,应用程序可能需要为不同的事件日志或不同的资源文件配置多个源。

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

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

注意

如果源已映射到日志,并且你将其重新映射到新日志,则必须重新启动计算机才能使更改生效。

另请参阅

适用于