EventLog.WriteEvent 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向事件日志写入本地化事件项。
重载
WriteEvent(String, EventInstance, Object[]) |
使用指定的已注册事件源写入包含给定事件数据和消息替换字符串的事件日志项。 |
WriteEvent(EventInstance, Object[]) |
向事件日志写入本地化项。 |
WriteEvent(EventInstance, Byte[], Object[]) |
写入包含给定的事件数据、消息替换字符串和关联的二进制数据的事件日志项。 |
WriteEvent(String, EventInstance, Byte[], Object[]) |
使用指定的已注册事件源,写入包含给定的事件数据、消息替换字符串和关联的二进制数据的事件日志项。 |
WriteEvent(String, EventInstance, Object[])
使用指定的已注册事件源写入包含给定事件数据和消息替换字符串的事件日志项。
public:
static void WriteEvent(System::String ^ source, System::Diagnostics::EventInstance ^ instance, ... cli::array <System::Object ^> ^ values);
public static void WriteEvent (string source, System.Diagnostics.EventInstance instance, params object[] values);
static member WriteEvent : string * System.Diagnostics.EventInstance * obj[] -> unit
Public Shared Sub WriteEvent (source As String, instance As EventInstance, ParamArray values As Object())
参数
- source
- String
在指定的计算机上为应用程序注册的事件源的名称。
- instance
- EventInstance
表示本地化事件日志项的 EventInstance 实例。
- values
- Object[]
要合并到事件日志项的消息文本中的字符串数组。
例外
source
值是空字符串 ("")。
或 -
source
值为null
。或 -
instance.InstanceId
小于零或大于 UInt16.MaxValue。或 -
values
超过了 256 个元素。或 -
values
元素中有一个超过了 32766 个字节。或 - 此源名称导致注册表项路径的长度超过 254 个字符。
instance
为 null
。
事件日志的注册表项未能打开。
将事件项写入事件日志时,操作系统报告了一个错误。 一个 Windows 错误代码不可用。
示例
以下示例将信息性事件条目和警告事件条目写入现有事件日志。 事件消息文本是在资源文件中使用资源标识符指定的。 该示例假定已为源注册相应的资源文件。
String^ sourceName = "SampleApplicationSource";
if ( EventLog::SourceExists( sourceName ) )
{
// Define an informational event and a warning event.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance ^ myInfoEvent = gcnew EventInstance( InformationMsgId,0,EventLogEntryType::Information );
EventInstance ^ myWarningEvent = gcnew EventInstance( WarningMsgId,0,EventLogEntryType::Warning );
// Insert the method name into the event log message.
array<String^>^insertStrings = {"EventLogSamples.WriteEventSample2"};
// Write the events to the event log.
EventLog::WriteEvent( sourceName, myInfoEvent, 0 );
// Append binary data to the warning event entry.
array<Byte>^binaryData = {7,8,9,10};
EventLog::WriteEvent( sourceName, myWarningEvent, binaryData, insertStrings );
}
else
{
Console::WriteLine( "Warning - event source {0} not registered", sourceName );
}
string sourceName = "SampleApplicationSource";
if(EventLog.SourceExists(sourceName))
{
// Define an informational event and a warning event.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance myInfoEvent = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information);
EventInstance myWarningEvent = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning);
// Insert the method name into the event log message.
string [] insertStrings = {"EventLogSamples.WriteEventSample2"};
// Write the events to the event log.
EventLog.WriteEvent(sourceName, myInfoEvent);
// Append binary data to the warning event entry.
byte [] binaryData = { 7, 8, 9, 10 };
EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings);
}
else
{
Console.WriteLine("Warning - event source {0} not registered",
sourceName);
}
Dim sourceName As String = "SampleApplicationSource"
If EventLog.SourceExists(sourceName)
' Define an informational event and a warning event.
' The message identifiers correspond to the message text in the
' message resource file defined for the source.
Dim myInfoEvent As EventInstance = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information)
Dim myWarningEvent As EventInstance = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning)
' Insert the method name into the event log message.
Dim insertStrings() As String = {"EventLogSamples.WriteEventSample2"}
' Write the events to the event log.
EventLog.WriteEvent(sourceName, myInfoEvent, insertStrings)
' Append binary data to the warning event entry.
Dim binaryData() As Byte = { 7, 8, 9, 10 }
EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings)
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
.
注解
使用此方法可将本地化条目写入事件日志,并使用已注册为相应日志的事件源的源。 使用资源标识符而不是字符串值指定事件属性。 事件查看器使用资源标识符显示源本地化资源文件中的相应字符串。 在使用资源标识符写入事件之前,必须先将源注册到相应的资源文件。
输入 instance
实例指定事件消息和属性。 设置 InstanceId 源消息资源文件中已定义消息的 instance
输入。 可以选择设置CategoryId输入和EntryTypeinstance
输入,以定义事件条目的类别和事件类型。 还可以指定要插入本地化消息文本中与语言无关的字符串数组。 Set values
to null
if the event message does not contain formatting placeholders for replacement strings.
在使用之前 WriteEvent,必须为事件日志注册指定的源。 必须配置指定的源,以便将本地化条目写入日志;源必须至少定义消息资源文件。
必须先创建和配置事件源,然后才能使用源编写第一个条目。 在安装应用程序期间创建新的事件源。 这样,操作系统就可以刷新其已注册事件源及其配置的列表。 如果操作系统尚未刷新其事件源列表,并且您尝试使用新源写入事件,则写入操作将失败。 可以使用或EventLogInstallerCreateEventSource方法配置新源。 您必须在计算机上具有管理权限才能创建新的事件源。
必须配置源以编写本地化条目或编写直接字符串。 WriteEntry如果应用程序将字符串值直接写入事件日志,请使用该方法。
如果应用程序使用资源标识符和字符串值编写条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后在方法中使用 WriteEvent 该源使用资源标识符将条目写入事件日志。 然后,在没有资源文件的情况下创建另一个源,并在方法中使用 WriteEntry 该源直接将字符串写入到使用该源的事件日志。
另请参阅
适用于
WriteEvent(EventInstance, Object[])
向事件日志写入本地化项。
public:
void WriteEvent(System::Diagnostics::EventInstance ^ instance, ... cli::array <System::Object ^> ^ values);
public void WriteEvent (System.Diagnostics.EventInstance instance, params object[] values);
[System.Runtime.InteropServices.ComVisible(false)]
public void WriteEvent (System.Diagnostics.EventInstance instance, params object[] values);
member this.WriteEvent : System.Diagnostics.EventInstance * obj[] -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.WriteEvent : System.Diagnostics.EventInstance * obj[] -> unit
Public Sub WriteEvent (instance As EventInstance, ParamArray values As Object())
参数
- instance
- EventInstance
表示本地化事件日志项的 EventInstance 实例。
- values
- Object[]
要合并到事件日志项的消息文本中的字符串数组。
- 属性
例外
或 - 该方法尝试注册新的事件源,但 MachineName 中的计算机名称无效。
或 - 已为其他事件日志注册了该源。
或 -
instance.InstanceId
小于零或大于 UInt16.MaxValue。或 -
values
超过了 256 个元素。或 -
values
元素中有一个超过了 32766 个字节。或 - 此源名称导致注册表项路径的长度超过 254 个字符。
instance
为 null
。
事件日志的注册表项未能打开。
将事件项写入事件日志时,操作系统报告了一个错误。 一个 Windows 错误代码不可用。
示例
以下示例将两个审核条目写入事件日志 myNewLog
。 如果本地计算机上不存在新事件源,该示例会创建一个新的事件源和一个新的事件日志。 事件消息文本是使用资源文件中的资源标识符指定的。
// Create the event source if it does not exist.
String^ sourceName = "SampleApplicationSource";
if ( !EventLog::SourceExists( sourceName ) )
{
// Call a local method to register the event log source
// for the event log "myNewLog." Use the resource file
// EventLogMsgs.dll in the current directory for message text.
String^ messageFile = String::Format( "{0}\\{1}", System::Environment::CurrentDirectory, "EventLogMsgs.dll" );
CreateEventSourceSample1( messageFile );
}
// Get the event log corresponding to the existing source.
String^ myLogName = EventLog::LogNameFromSourceName( sourceName, "." );
EventLog^ myEventLog = gcnew EventLog( myLogName,".",sourceName );
// Define two audit events.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance ^ myAuditSuccessEvent = gcnew EventInstance( AuditSuccessMsgId,0,EventLogEntryType::SuccessAudit );
EventInstance ^ myAuditFailEvent = gcnew EventInstance( AuditFailedMsgId,0,EventLogEntryType::FailureAudit );
// Insert the method name into the event log message.
array<String^>^insertStrings = {"EventLogSamples.WriteEventSample1"};
// Write the events to the event log.
myEventLog->WriteEvent( myAuditSuccessEvent, insertStrings );
// Append binary data to the audit failure event entry.
array<Byte>^binaryData = {3,4,5,6};
myEventLog->WriteEvent( myAuditFailEvent, binaryData, insertStrings );
// Create the event source if it does not exist.
string sourceName = "SampleApplicationSource";
if(!EventLog.SourceExists(sourceName))
{
// Call a local method to register the event log source
// for the event log "myNewLog." Use the resource file
// EventLogMsgs.dll in the current directory for message text.
string messageFile = String.Format("{0}\\{1}",
System.Environment.CurrentDirectory,
"EventLogMsgs.dll");
CreateEventSourceSample1(messageFile);
}
// Get the event log corresponding to the existing source.
string myLogName = EventLog.LogNameFromSourceName(sourceName,".");
EventLog myEventLog = new EventLog(myLogName, ".", sourceName);
// Define two audit events.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance myAuditSuccessEvent = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit);
EventInstance myAuditFailEvent = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit);
// Insert the method name into the event log message.
string [] insertStrings = {"EventLogSamples.WriteEventSample1"};
// Write the events to the event log.
myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings);
// Append binary data to the audit failure event entry.
byte [] binaryData = { 3, 4, 5, 6 };
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings);
Dim sourceName As String = "SampleApplicationSource"
' Create the event source if it does not exist.
If Not EventLog.SourceExists(sourceName)
' Call a local method to register the event log source
' for the event log "myNewLog." Use the resource file
' EventLogMsgs.dll in the current directory for message text.
Dim messageFile As String = String.Format("{0}\\{1}", _
System.Environment.CurrentDirectory, _
"EventLogMsgs.dll")
CreateEventSourceSample1(messageFile)
End If
' Get the event log corresponding to the existing source.
Dim myLogName As String = EventLog.LogNameFromSourceName(sourceName,".")
Dim myEventLog As EventLog = new EventLog(myLogName, ".", sourceName)
' Define two audit events.
Dim myAuditSuccessEvent As EventInstance = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit)
Dim myAuditFailEvent As EventInstance = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit)
' Insert the method name into the event log message.
Dim insertStrings() As String = {"EventLogSamples.WriteEventSample1"}
' Write the events to the event log.
myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings)
' Append binary data to the audit failure event entry.
Dim binaryData() As Byte = { 7, 8, 9, 10 }
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings)
此示例使用以下消息文本文件,该文件内置于资源库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
.
注解
使用此方法将本地化条目写入事件日志。 使用资源标识符而不是字符串值指定事件属性。 事件查看器使用资源标识符显示本地化资源文件中的Source相应字符串。 在使用资源标识符编写事件之前,必须先将源注册到相应的资源文件。
输入 instance
实例指定事件消息和属性。 设置InstanceIdinstance
源消息资源文件中已定义消息的输入。 可以选择设置CategoryId输入和EntryTypeinstance
输入,以定义事件条目的类别和事件类型。 还可以指定要插入到本地化消息文本中的独立于语言的字符串数组。 null
如果事件消息不包含替换字符串的格式占位符,则设置为values
该设置。
在使用之前WriteEvent,必须在组件上EventLog设置Source属性。 必须配置指定的源,以便将本地化条目写入日志;源必须至少定义消息资源文件。
必须先创建和配置事件源,然后才能使用源编写第一个条目。 在安装应用程序期间创建新的事件源。 这样,操作系统就可以刷新其已注册事件源及其配置的列表。 如果操作系统尚未刷新其事件源列表,并且您尝试使用新源写入事件,则写入操作将失败。 可以使用或EventLogInstallerCreateEventSource方法配置新源。 必须在计算机上拥有管理权限才能创建新的事件源。
必须配置源以编写本地化条目或编写直接字符串。 WriteEntry如果应用程序将字符串值直接写入事件日志,请使用此方法。
如果应用程序使用资源标识符和字符串值写入条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后使用该方法中的 WriteEvent 源将资源标识符用于将条目写入事件日志。 然后创建一个不带资源文件的其他源,并在方法中使用 WriteEntry 该源直接将字符串写入到使用该源的事件日志。
备注
如果将条目写入远程计算机,则如果远程计算机未运行.NET Framework,则字符串的值message
可能不是预期值。 此外, message
字符串不能包含 %n,其中 n 是一个整数值 (,例如 %1) ,因为事件查看器将其视为插入字符串。 由于 Internet 协议版本 6 (IPv6) 地址可以包含此字符序列,因此无法记录包含 IPv6 地址的事件消息。
另请参阅
适用于
WriteEvent(EventInstance, Byte[], Object[])
写入包含给定的事件数据、消息替换字符串和关联的二进制数据的事件日志项。
public:
void WriteEvent(System::Diagnostics::EventInstance ^ instance, cli::array <System::Byte> ^ data, ... cli::array <System::Object ^> ^ values);
public void WriteEvent (System.Diagnostics.EventInstance instance, byte[] data, params object[] values);
[System.Runtime.InteropServices.ComVisible(false)]
public void WriteEvent (System.Diagnostics.EventInstance instance, byte[] data, params object[] values);
member this.WriteEvent : System.Diagnostics.EventInstance * byte[] * obj[] -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.WriteEvent : System.Diagnostics.EventInstance * byte[] * obj[] -> unit
Public Sub WriteEvent (instance As EventInstance, data As Byte(), ParamArray values As Object())
参数
- instance
- EventInstance
表示本地化事件日志项的 EventInstance 实例。
- data
- Byte[]
包含与此项关联的二进制数据的字节数组。
- values
- Object[]
要合并到事件日志项的消息文本中的字符串数组。
- 属性
例外
或 - 该方法尝试注册新的事件源,但 MachineName 中的计算机名称无效。
或 - 已为其他事件日志注册了该源。
或 -
instance.InstanceId
小于零或大于 UInt16.MaxValue。或 -
values
超过了 256 个元素。或 -
values
元素中有一个超过了 32766 个字节。或 - 此源名称导致注册表项路径的长度超过 254 个字符。
instance
为 null
。
事件日志的注册表项未能打开。
将事件项写入事件日志时,操作系统报告了一个错误。 一个 Windows 错误代码不可用。
示例
以下示例将两个审核条目写入事件日志 myNewLog
。 如果本地计算机上不存在新的事件源和新的事件日志,该示例将创建一个新的事件源和一个新的事件日志。 事件消息文本是在资源文件中使用资源标识符指定的。
// Create the event source if it does not exist.
String^ sourceName = "SampleApplicationSource";
if ( !EventLog::SourceExists( sourceName ) )
{
// Call a local method to register the event log source
// for the event log "myNewLog." Use the resource file
// EventLogMsgs.dll in the current directory for message text.
String^ messageFile = String::Format( "{0}\\{1}", System::Environment::CurrentDirectory, "EventLogMsgs.dll" );
CreateEventSourceSample1( messageFile );
}
// Get the event log corresponding to the existing source.
String^ myLogName = EventLog::LogNameFromSourceName( sourceName, "." );
EventLog^ myEventLog = gcnew EventLog( myLogName,".",sourceName );
// Define two audit events.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance ^ myAuditSuccessEvent = gcnew EventInstance( AuditSuccessMsgId,0,EventLogEntryType::SuccessAudit );
EventInstance ^ myAuditFailEvent = gcnew EventInstance( AuditFailedMsgId,0,EventLogEntryType::FailureAudit );
// Insert the method name into the event log message.
array<String^>^insertStrings = {"EventLogSamples.WriteEventSample1"};
// Write the events to the event log.
myEventLog->WriteEvent( myAuditSuccessEvent, insertStrings );
// Append binary data to the audit failure event entry.
array<Byte>^binaryData = {3,4,5,6};
myEventLog->WriteEvent( myAuditFailEvent, binaryData, insertStrings );
// Create the event source if it does not exist.
string sourceName = "SampleApplicationSource";
if(!EventLog.SourceExists(sourceName))
{
// Call a local method to register the event log source
// for the event log "myNewLog." Use the resource file
// EventLogMsgs.dll in the current directory for message text.
string messageFile = String.Format("{0}\\{1}",
System.Environment.CurrentDirectory,
"EventLogMsgs.dll");
CreateEventSourceSample1(messageFile);
}
// Get the event log corresponding to the existing source.
string myLogName = EventLog.LogNameFromSourceName(sourceName,".");
EventLog myEventLog = new EventLog(myLogName, ".", sourceName);
// Define two audit events.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance myAuditSuccessEvent = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit);
EventInstance myAuditFailEvent = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit);
// Insert the method name into the event log message.
string [] insertStrings = {"EventLogSamples.WriteEventSample1"};
// Write the events to the event log.
myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings);
// Append binary data to the audit failure event entry.
byte [] binaryData = { 3, 4, 5, 6 };
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings);
Dim sourceName As String = "SampleApplicationSource"
' Create the event source if it does not exist.
If Not EventLog.SourceExists(sourceName)
' Call a local method to register the event log source
' for the event log "myNewLog." Use the resource file
' EventLogMsgs.dll in the current directory for message text.
Dim messageFile As String = String.Format("{0}\\{1}", _
System.Environment.CurrentDirectory, _
"EventLogMsgs.dll")
CreateEventSourceSample1(messageFile)
End If
' Get the event log corresponding to the existing source.
Dim myLogName As String = EventLog.LogNameFromSourceName(sourceName,".")
Dim myEventLog As EventLog = new EventLog(myLogName, ".", sourceName)
' Define two audit events.
Dim myAuditSuccessEvent As EventInstance = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit)
Dim myAuditFailEvent As EventInstance = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit)
' Insert the method name into the event log message.
Dim insertStrings() As String = {"EventLogSamples.WriteEventSample1"}
' Write the events to the event log.
myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings)
' Append binary data to the audit failure event entry.
Dim binaryData() As Byte = { 7, 8, 9, 10 }
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings)
该示例使用以下消息文本文件,内置于资源库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
.
注解
使用此方法可将具有其他特定于事件的本地化条目写入事件日志。 使用资源标识符而不是字符串值指定事件属性。 事件查看器使用资源标识符显示本地化资源文件中的Source相应字符串。 在使用资源标识符编写事件之前,必须先将源注册到相应的资源文件。
输入 instance
实例指定事件消息和属性。 设置InstanceIdinstance
源消息资源文件中已定义消息的输入。 可以选择设置CategoryId输入和EntryTypeinstance
输入,以定义事件条目的类别和事件类型。 还可以指定要插入到本地化消息文本中的独立于语言的字符串数组。 null
如果事件消息不包含替换字符串的格式占位符,则设置为values
该设置。
如果需要为事件提供其他详细信息,请使用事件指定二进制数据。 例如,使用 data
参数包含有关特定错误的信息。 事件查看器不解释关联的事件数据;它以组合的十六进制和文本格式显示数据。 谨慎使用特定于事件的数据;仅当确定它非常有用时,才包含它。 还可以使用特定于事件的数据来存储应用程序可以独立于事件查看器处理的信息。 例如,可以专门为事件编写查看器,或编写扫描事件日志的程序,并创建包含事件特定数据中信息的报表。
在使用组件之前, Source 必须在组件上 EventLog 设置 WriteEvent属性。 必须配置指定的源,以便将本地化条目写入日志;源必须至少定义消息资源文件。
必须先创建和配置事件源,然后才能使用源编写第一个条目。 在安装应用程序期间创建新的事件源。 这样,操作系统就可以刷新其已注册事件源及其配置的列表。 如果操作系统尚未刷新其事件源列表,并且您尝试使用新源写入事件,则写入操作将失败。 可以使用或EventLogInstallerCreateEventSource方法配置新源。 必须在计算机上拥有管理权限才能创建新的事件源。
备注
如果在调用WriteEvent之前未为EventLog实例指定一个MachineName实例,则本地计算机 (“。”假定) 。
必须配置源以编写本地化条目或编写直接字符串。 WriteEntry如果应用程序将字符串值直接写入事件日志,请使用此方法。
如果应用程序使用资源标识符和字符串值写入条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后在方法中使用 WriteEvent 该源使用资源标识符将条目写入事件日志。 然后,在没有资源文件的情况下创建另一个源,并在方法中使用 WriteEntry 该源直接将字符串写入到使用该源的事件日志。
备注
如果将条目写入远程计算机,则如果远程计算机未运行.NET Framework,字符串的值message
可能不是预期值。 此外, message
字符串不能包含 %n,其中 n 是整数值 (,例如 %1) ,因为事件查看器将其视为插入字符串。 由于 Internet 协议版本 6 (IPv6) 地址可以包含此字符序列,因此无法记录包含 IPv6 地址的事件消息。
另请参阅
适用于
WriteEvent(String, EventInstance, Byte[], Object[])
使用指定的已注册事件源,写入包含给定的事件数据、消息替换字符串和关联的二进制数据的事件日志项。
public:
static void WriteEvent(System::String ^ source, System::Diagnostics::EventInstance ^ instance, cli::array <System::Byte> ^ data, ... cli::array <System::Object ^> ^ values);
public static void WriteEvent (string source, System.Diagnostics.EventInstance instance, byte[] data, params object[] values);
static member WriteEvent : string * System.Diagnostics.EventInstance * byte[] * obj[] -> unit
Public Shared Sub WriteEvent (source As String, instance As EventInstance, data As Byte(), ParamArray values As Object())
参数
- source
- String
在指定的计算机上为应用程序注册的事件源的名称。
- instance
- EventInstance
表示本地化事件日志项的 EventInstance 实例。
- data
- Byte[]
包含与此项关联的二进制数据的字节数组。
- values
- Object[]
要合并到事件日志项的消息文本中的字符串数组。
例外
source
值是空字符串 ("")。
或 -
source
值为null
。或 -
instance.InstanceId
小于零或大于 UInt16.MaxValue。或 -
values
超过了 256 个元素。或 -
values
元素中有一个超过了 32766 个字节。或 - 此源名称导致注册表项路径的长度超过 254 个字符。
instance
为 null
。
事件日志的注册表项未能打开。
将事件项写入事件日志时,操作系统报告了一个错误。 一个 Windows 错误代码不可用。
示例
以下示例将信息性事件条目和警告事件条目写入现有事件日志。 事件消息文本是使用资源文件中的资源标识符指定的。 该示例假定已为源注册相应的资源文件。
String^ sourceName = "SampleApplicationSource";
if ( EventLog::SourceExists( sourceName ) )
{
// Define an informational event and a warning event.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance ^ myInfoEvent = gcnew EventInstance( InformationMsgId,0,EventLogEntryType::Information );
EventInstance ^ myWarningEvent = gcnew EventInstance( WarningMsgId,0,EventLogEntryType::Warning );
// Insert the method name into the event log message.
array<String^>^insertStrings = {"EventLogSamples.WriteEventSample2"};
// Write the events to the event log.
EventLog::WriteEvent( sourceName, myInfoEvent, 0 );
// Append binary data to the warning event entry.
array<Byte>^binaryData = {7,8,9,10};
EventLog::WriteEvent( sourceName, myWarningEvent, binaryData, insertStrings );
}
else
{
Console::WriteLine( "Warning - event source {0} not registered", sourceName );
}
string sourceName = "SampleApplicationSource";
if(EventLog.SourceExists(sourceName))
{
// Define an informational event and a warning event.
// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance myInfoEvent = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information);
EventInstance myWarningEvent = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning);
// Insert the method name into the event log message.
string [] insertStrings = {"EventLogSamples.WriteEventSample2"};
// Write the events to the event log.
EventLog.WriteEvent(sourceName, myInfoEvent);
// Append binary data to the warning event entry.
byte [] binaryData = { 7, 8, 9, 10 };
EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings);
}
else
{
Console.WriteLine("Warning - event source {0} not registered",
sourceName);
}
Dim sourceName As String = "SampleApplicationSource"
If EventLog.SourceExists(sourceName)
' Define an informational event and a warning event.
' The message identifiers correspond to the message text in the
' message resource file defined for the source.
Dim myInfoEvent As EventInstance = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information)
Dim myWarningEvent As EventInstance = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning)
' Insert the method name into the event log message.
Dim insertStrings() As String = {"EventLogSamples.WriteEventSample2"}
' Write the events to the event log.
EventLog.WriteEvent(sourceName, myInfoEvent, insertStrings)
' Append binary data to the warning event entry.
Dim binaryData() As Byte = { 7, 8, 9, 10 }
EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings)
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
.
注解
使用此方法可将具有附加事件特定数据的本地化条目写入事件日志,并使用已注册为相应日志的事件源的源。 使用资源标识符而不是字符串值指定事件属性。 事件查看器使用资源标识符显示源本地化资源文件中的相应字符串。 在使用资源标识符写入事件之前,必须先将源注册到相应的资源文件。
输入 instance
实例指定事件消息和属性。 设置 InstanceId 源消息资源文件中已定义消息的 instance
输入。 可以选择设置CategoryId输入和EntryTypeinstance
输入,以定义事件条目的类别和事件类型。 还可以指定要插入本地化消息文本中与语言无关的字符串数组。 Set values
to null
if the event message does not contain formatting placeholders for replacement strings.
如果需要为事件提供其他详细信息,请使用事件指定二进制数据。 例如,使用 data
参数包含有关特定错误的信息。 事件查看器不解释关联的事件数据;它以组合的十六进制和文本格式显示数据。 谨慎使用特定于事件的数据;仅当确定它会很有用时,才包含它。 还可以使用特定于事件的数据来存储应用程序可以独立于事件查看器处理的信息。 例如,可以专门为事件编写查看器,或编写扫描事件日志的程序,并创建包含事件特定数据信息的报告。
在使用之前 WriteEvent,必须为事件日志注册指定的源。 必须配置指定的源,以便将本地化条目写入日志;源必须至少定义消息资源文件。
必须先创建和配置事件源,然后才能使用源编写第一个条目。 在安装应用程序期间创建新的事件源。 这样,操作系统就可以刷新其已注册事件源及其配置的列表。 如果操作系统尚未刷新其事件源列表,并且您尝试使用新源写入事件,则写入操作将失败。 可以使用或EventLogInstallerCreateEventSource方法配置新源。 必须在计算机上拥有管理权限才能创建新的事件源。
必须配置源以编写本地化条目或编写直接字符串。 WriteEntry如果应用程序将字符串值直接写入事件日志,请使用此方法。
如果应用程序使用资源标识符和字符串值写入条目,则必须注册两个单独的源。 例如,使用资源文件配置一个源,然后使用该方法中的 WriteEvent 源将资源标识符用于将条目写入事件日志。 然后创建一个不带资源文件的其他源,并在方法中使用 WriteEntry 该源直接将字符串写入到使用该源的事件日志。