EventInstance 类

定义

表示事件日志项的非特定语言信息。

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

示例

下面的代码示例编写一个信息性事件条目,然后重复使用 EventInstance 以将警告事件的条目写入现有事件日志。 事件消息文本是使用消息资源文件中的资源标识符指定的。 代码示例假定已为源注册相应的消息资源文件。

// Ensure that the source has already been registered using
// EventLogInstaller or EventLog.CreateEventSource.
String^ sourceName = "SampleApplicationSource";
if ( EventLog::SourceExists( sourceName ) )
{
   // Define an informational event with no category.
   // The message identifier corresponds to the message text in the
   // message resource file defined for the source.
   EventInstance ^ myEvent = gcnew EventInstance( UpdateCycleCompleteMsgId,0 );

   // Write the event to the event log using the registered source.
   EventLog::WriteEvent( sourceName, myEvent, 0 );

   // Reuse the event data instance for another event entry.
   // Set the entry category and message identifiers for
   // the appropriate resource identifiers in the resource files
   // for the registered source.  Set the event type to Warning.
   myEvent->CategoryId = RefreshCategoryMsgId;
   myEvent->EntryType = EventLogEntryType::Warning;
   myEvent->InstanceId = ServerConnectionDownMsgId;

   // Write the event to the event log using the registered source.
   // Insert the machine name into the event message text.
   array<String^>^ss = {Environment::MachineName};
   EventLog::WriteEvent( sourceName, myEvent, ss );
}
else
{
   Console::WriteLine( "Warning - event source {0} not registered", sourceName );
}

// Ensure that the source has already been registered using
// EventLogInstaller or EventLog.CreateEventSource.

string sourceName = "SampleApplicationSource";
if(EventLog.SourceExists(sourceName))
{
    // Define an informational event with no category.
    // The message identifier corresponds to the message text in the
    // message resource file defined for the source.
    EventInstance myEvent = new EventInstance(UpdateCycleCompleteMsgId, 0);

    // Write the event to the event log using the registered source.
    EventLog.WriteEvent(sourceName, myEvent);

    // Reuse the event data instance for another event entry.
    // Set the entry category and message identifiers for
    // the appropriate resource identifiers in the resource files
    // for the registered source.  Set the event type to Warning.

    myEvent.CategoryId = RefreshCategoryMsgId;
    myEvent.EntryType = EventLogEntryType.Warning;
    myEvent.InstanceId = ServerConnectionDownMsgId;

    // Write the event to the event log using the registered source.
    // Insert the machine name into the event message text.
    EventLog.WriteEvent(sourceName, myEvent, Environment.MachineName);
}
else
{
    Console.WriteLine("Warning - event source {0} not registered",
        sourceName);
}
' Ensure that the source has already been registered using
' EventLogInstaller or EventLog.CreateEventSource.
Dim sourceName as String = "SampleApplicationSource"
If EventLog.SourceExists(sourceName)
    
    ' Define an informational event with no category.
    ' The message identifier corresponds to the message text in the
    ' message resource file defined for the source.
    Dim myEvent As EventInstance = New EventInstance(UpdateCycleCompleteMsgId, 0)
    ' Write the event to the event log using the registered source.
    EventLog.WriteEvent(sourceName, myEvent)

    ' Reuse the event data instance for another event entry.
    ' Set the entry category and message identifiers for
    ' the appropriate resource identifiers in the resource files
    ' for the registered source.  Set the event type to Warning.

    myEvent.CategoryId = RefreshCategoryMsgId
    myEvent.EntryType = EventLogEntryType.Warning
    myEvent.InstanceId = ServerConnectionDownMsgId

    ' Write the event to the event log using the registered source.
    ' Insert the machine name into the event message text.
    EventLog.WriteEvent(sourceName, myEvent, Environment.MachineName)

Else 
    Console.WriteLine("Warning - event source {0} not registered", _
        sourceName)
End If

该代码示例使用以下消息文本文件,该文件内置于资源库中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  
.  

注解

使用 EventInstance 编写包含资源标识符而不是字符串值的事件日志条目。 若要写入事件日志条目,请 InstanceId 初始化 属性并将 实例传递给 WriteEvent 方法。 事件查看器使用实例标识符根据当前语言设置查找并显示本地化资源文件中的相应字符串。 在使用资源标识符编写事件之前,必须先将事件源注册到相应的资源文件。

写入事件时,可以设置 EntryType 属性以指定事件查看器为条目显示的图标。 还可以指定属性CategoryId以指定事件查看器为条目显示的类别。

事件查看器使用 类别来筛选由事件源编写的事件。 事件查看器可以将类别显示为数值,也可以使用类别作为资源标识符来显示本地化的类别字符串。

若要在事件查看器中显示本地化类别字符串,必须使用使用类别资源文件配置的事件源,并将 设置为CategoryId类别资源文件中的资源标识符。 如果事件源没有配置的类别资源文件,或者指定的 CategoryId 未为类别资源文件中的字符串编制索引,则事件查看器显示该条目的数值类别值。 使用 EventLogInstallerEventSourceCreationData 类配置类别资源文件以及资源文件中的类别字符串数。

必须先为事件日志注册源,然后才能使用 EventInstance 以使用 WriteEvent 方法编写事件。 必须将源配置为将本地化条目写入日志,并且源必须至少定义消息资源文件。

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

有关定义事件消息和生成事件日志资源文件的详细信息,请参阅平台 SDK 文档中 的消息编译器 一文。

构造函数

EventInstance(Int64, Int32)

使用事件项的本地化消息和类别文本的指定资源标识符初始化 EventInstance 类的一个新实例。

EventInstance(Int64, Int32, EventLogEntryType)

使用事件项的本地化消息和类别文本的指定资源标识符以及指定的事件日志项类型初始化 EventInstance 类的一个新实例。

属性

CategoryId

获取或设置指定事件项的应用程序定义类别的资源标识符。

EntryType

获取或设置事件日志项的事件类型。

InstanceId

获取或设置指定事件项的消息文本的资源标识符。

方法

Equals(Object)

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

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)

适用于

另请参阅