EventLog.CreateEventSource 方法

定义

建立应用程序,以便将事件信息写入系统上的特定日志。

重载

CreateEventSource(EventSourceCreationData)

使用事件源和相应的事件日志的指定配置属性,建立用于编写本地化事件消息的有效事件源。

CreateEventSource(String, String)

将指定的源名称建立为有效的事件源,以便将条目写入本地计算机上的日志。 此方法还可以在本地计算机上创建新的自定义日志。

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

将指定的源名称建立为有效的事件源,以便将条目写入指定计算机上的日志。 此方法还可用于在指定计算机上创建新的自定义日志。

CreateEventSource(EventSourceCreationData)

Source:
EventLog.cs
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 中指定的源名称与现有的事件日志名称匹配。

无法打开事件日志的注册表项。

sourceData null

示例

以下示例确定名为 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)确定用户的权限。 如果你是内置管理员组的成员,则分配有两个运行时访问令牌:标准用户访问令牌和管理员访问令牌。 默认情况下,你处于标准用户角色。 若要执行访问安全日志的代码,必须先将特权从标准用户提升到管理员。 在启动应用程序时,可以右键单击应用程序图标并指示要以管理员身份运行。

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

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

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

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

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

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

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

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

注意

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

另请参阅

适用于

CreateEventSource(String, String)

Source:
EventLog.cs
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

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

注意

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

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

在 Windows Vista 及更高版本中,用户帐户控制(UAC)确定用户的权限。 如果你是内置管理员组的成员,则分配有两个运行时访问令牌:标准用户访问令牌和管理员访问令牌。 默认情况下,你处于标准用户角色。 若要执行访问安全日志的代码,必须先将特权从标准用户提升到管理员。 在启动应用程序时,可以右键单击应用程序图标并指示要以管理员身份运行。

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

注意

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

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

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

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

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

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

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

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

注意

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

另请参阅

适用于

CreateEventSource(String, String, String)

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

注意

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

注意

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

将指定的源名称建立为有效的事件源,以便将条目写入指定计算机上的日志。 此方法还可用于在指定计算机上创建新的自定义日志。

public:
 static void CreateEventSource(System::String ^ source, System::String ^ logName, System::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")]
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.")>]
static member CreateEventSource : string * string * string -> unit
static member CreateEventSource : string * string * string -> unit
[<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
Public Shared Sub CreateEventSource (source As String, logName As String, machineName As String)

参数

source
String

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

logName
String

源条目要写入的日志的名称。 可能的值包括应用程序、系统或自定义事件日志。 如果未指定值,logName 默认为 Application。

machineName
String

要向本地计算机注册此事件源的计算机的名称,或“.”。

属性

例外

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

-或-

source 为空字符串(“”)或 null

-或-

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

-或-

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

-或-

日志名称与现有事件源名称匹配。

-或-

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

-或-

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

-或-

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

-或-

源名称与现有事件源名称匹配。

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

示例

以下示例在计算机上创建源 MySourceMyServer,并将条目写入事件日志 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", "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 并将其注册到指定计算机上的现有日志。

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

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

注意

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

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

在 Windows Vista 及更高版本中,用户帐户控制(UAC)确定用户的权限。 如果你是内置管理员组的成员,则分配有两个运行时访问令牌:标准用户访问令牌和管理员访问令牌。 默认情况下,你处于标准用户角色。 若要执行访问安全日志的代码,必须先将特权从标准用户提升到管理员。 在启动应用程序时,可以右键单击应用程序图标并指示要以管理员身份运行。

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

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

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

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

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

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

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

注意

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

另请参阅

适用于