Share via


EventInstance.CategoryId Propiedad

Definición

Obtiene o establece el identificador de recursos que especifica la categoría definida por la aplicación de la entrada de evento.

public:
 property int CategoryId { int get(); void set(int value); };
public int CategoryId { get; set; }
member this.CategoryId : int with get, set
Public Property CategoryId As Integer

Valor de propiedad

Valor de categoría numérico o identificador de recursos correspondiente a una cadena definida en el archivo de recursos de categoría del origen de eventos. El valor predeterminado es cero, lo que significa que no se mostrará ninguna categoría para la entrada de evento.

Excepciones

La propiedad se establece en un valor negativo o en un valor mayor que UInt16.MaxValue.

Ejemplos

En el ejemplo de código siguiente se escribe una entrada de evento informativo y, a continuación, se reutiliza para EventInstance escribir una entrada para un evento de advertencia en un registro de eventos existente. El texto del mensaje de evento se especifica mediante un identificador de recurso en un archivo de recursos de mensaje. En el ejemplo de código se supone que el archivo de recursos del mensaje correspondiente se ha registrado para el origen.

// 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

En el ejemplo de código se usa el siguiente archivo de texto de mensaje, integrado en la biblioteca de recursos EventLogMsgs.dll. Un archivo de texto de mensaje es el origen desde el que se crea el archivo de recursos del mensaje. El archivo de texto del mensaje define los identificadores de recursos y el texto de la categoría, el mensaje de evento y las cadenas de inserción de parámetros.

; // 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  
.  

Comentarios

Las categorías del registro de eventos son valores definidos por la aplicación que ayudan a filtrar eventos o proporcionan más información sobre el evento. Por ejemplo, la aplicación puede definir categorías independientes para distintos componentes o operaciones diferentes.

Establezca la CategoryId propiedad para especificar la categoría que muestra el Visor de eventos para la entrada. El Visor de eventos puede mostrar la categoría como un valor numérico, o bien puede usar como CategoryId identificador de recurso para mostrar una cadena de categoría localizada en función de la configuración de idioma actual.

Para mostrar cadenas de categoría localizadas en el Visor de eventos, debe usar un origen de eventos configurado con un archivo de recursos de categoría y establecer en CategoryId un identificador de recurso en el archivo de recursos de categoría. Si el origen del evento no tiene un archivo de recursos de categoría configurado o el especificado CategoryId no indexa una cadena en el archivo de recursos de categoría y, a continuación, el Visor de eventos muestra el valor numérico de categoría para esa entrada.

Debe registrar el origen con el archivo de recursos correspondiente antes de escribir categorías de eventos mediante identificadores de recursos. Configure el archivo de recursos de categoría, junto con el número de cadenas de categoría en el archivo de recursos, mediante o EventLogInstaller la EventSourceCreationData clase . Al definir cadenas de categoría en un archivo de recursos, los identificadores de recursos de categoría se deben numerar consecutivamente a partir de 1, hasta el valor de propiedad configurado CategoryCount .

Las categorías de eventos son opcionales. Si la aplicación no usa categorías, no establezca para CategoryId la entrada del registro de eventos.

Para más información sobre cómo definir mensajes de eventos y compilar archivos de recursos de eventos, consulte el artículo Compilador de mensajes en la documentación del SDK de plataforma. Para obtener más información sobre cómo definir categorías de eventos en archivos de recursos, consulte el tema "Categorías de eventos" en Platform SDK.

Se aplica a

Consulte también