EventSourceCreationData(String, String) 构造函数   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定的事件源和事件日志名称对 EventSourceCreationData 类的新实例进行初始化。
public:
 EventSourceCreationData(System::String ^ source, System::String ^ logName);public EventSourceCreationData (string source, string logName);new System.Diagnostics.EventSourceCreationData : string * string -> System.Diagnostics.EventSourceCreationDataPublic Sub New (source As String, logName As String)参数
- source
- String
要在事件日志中注册为项源的名称。
- logName
- String
要将来自源的项写入其中的日志的名称。
示例
下面的代码示例确定是否在本地计算机上注册了名为 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。 消息文本文件是创建消息资源文件的源。 消息文本文件定义类别、事件消息和参数插入字符串的资源标识符和文本。
; // 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  
.  
注解
若要将条目写入事件日志,必须先为事件日志创建事件源。 若要注册新源,请初始化 实例 EventSourceCreationData ,配置应用程序的实例属性,然后调用 EventLog.CreateEventSource(EventSourceCreationData) 方法。 注册源后,可以使用 或 WriteEntry 方法从源WriteEvent写入条目。
可以使用事件类别和消息字符串的本地化资源注册事件源。 应用程序可以使用资源标识符写入事件日志条目,而不是指定实际字符串。 事件查看器使用资源标识符根据当前语言设置查找并显示本地化资源文件中的相应字符串。 可以为事件类别、消息和参数插入字符串注册单独的文件,也可以为所有三种类型的字符串注册相同的资源文件。 CategoryCount使用 、CategoryResourceFile、 MessageResourceFile和 ParameterResourceFile 属性将源配置为将本地化条目写入事件日志。 如果应用程序将字符串值直接写入事件日志,则无需设置这些属性。
必须为编写本地化条目或写入直接字符串配置源。 如果应用程序使用资源标识符和字符串值写入条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后在 方法中 WriteEvent 使用该源将使用资源标识符的条目写入事件日志。 然后创建不带资源文件的其他源,并在 方法中 WriteEntry 使用该源将字符串直接写入事件日志。
下表显示了 的初始 EventSourceCreationData属性值。
| properties | 初始值 | 
|---|---|
| Source | source参数。 | 
| LogName | logName参数。 | 
| MachineName | 本地计算机 (“。”) 。 | 
| CategoryCount | 零 | 
| CategoryResourceFile | null(在 Visual Basic 中为Nothing)。 | 
| MessageResourceFile | null(在 Visual Basic 中为Nothing)。 | 
| ParameterResourceFile | null(在 Visual Basic 中为Nothing)。 |