編輯

共用方式為


EventLog.WriteEntry Method

Definition

Writes an entry in the event log.

Overloads

WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[])

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log (using the specified registered event source) and appends binary data to the message.

WriteEntry(String, String, EventLogEntryType, Int32, Int16)

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, using the specified registered event source. The category can be used by the Event Viewer to filter events in the log.

WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[])

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, and appends binary data to the message.

WriteEntry(String, String, EventLogEntryType, Int32)

Writes an entry with the given message text and application-defined event identifier to the event log, using the specified registered event source.

WriteEntry(String, EventLogEntryType, Int32, Int16)

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log.

WriteEntry(String, EventLogEntryType)

Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log.

WriteEntry(String, EventLogEntryType, Int32)

Writes an entry with the given message text and application-defined event identifier to the event log.

WriteEntry(String, String)

Writes an information type entry with the given message text to the event log, using the specified registered event source.

WriteEntry(String)

Writes an information type entry, with the given message text, to the event log.

WriteEntry(String, String, EventLogEntryType)

Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log, using the specified registered event source.

WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[])

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

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log (using the specified registered event source) and appends binary data to the message.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category, cli::array <System::Byte> ^ rawData);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type, int eventID, short category, byte[] rawData);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType * int * int16 * byte[] -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType, eventID As Integer, category As Short, rawData As Byte())

Parameters

source
String

The source by which the application is registered on the specified computer.

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

eventID
Int32

The application-specific identifier for the event.

category
Int16

The application-specific subcategory associated with the message.

rawData
Byte[]

An array of bytes that holds the binary data associated with the entry.

Exceptions

The source value is an empty string ("").

-or-

The source value is null.

-or-

eventID is less than zero or greater than UInt16.MaxValue.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

//Create a byte array for binary data to associate with the entry.
array<Byte>^myByte = gcnew array<Byte>(10);
//Populate the byte array with simulated data.
for ( int i = 0; i < 10; i++ )
{
   myByte[ i ] = (Byte)(i % 2);
}
//Write an entry to the event log that includes associated binary data.
Console::WriteLine( "Write from second source " );
EventLog::WriteEntry( "SecondSource", "Writing warning to event log.",
   EventLogEntryType::Error, myEventID, myCategory, myByte );
//Create a byte array for binary data to associate with the entry.
byte[] myByte = new byte[10];
//Populate the byte array with simulated data.
for (int i = 0; i < 10; i++)
{
    myByte[i] = (byte)(i % 2);
}
// Write an entry to the event log that includes associated binary data.
Console.WriteLine("Write from second source ");
EventLog.WriteEntry("SecondSource", "Writing warning to event log.",
                     EventLogEntryType.Error, myEventID, myCategory, myByte);
' Create a byte array for binary data to associate with the entry.
Dim myByte(9) As Byte
Dim i As Integer
' Populate the byte array with simulated data.
For i = 0 To 9
    myByte(i) = CByte(i Mod 2)
Next i
' Write an entry to the event log that includes associated binary data.
Console.WriteLine("Write from second source ")
EventLog.WriteEntry("SecondSource", "Writing warning to event log.", _
                     EventLogEntryType.Error, myEventID, myCategory, myByte)

Remarks

Use this method to write application-defined event-specific data to the event log, using a source already registered as an event source for the appropriate log. The Event Viewer does not interpret this data; it displays raw data only in a combined hexadecimal and text format. Use event-specific data sparingly; include it only if you are sure it will be useful. You can also use event-specific data to store information the application can process independently of the Event Viewer. For example, you could write a viewer specifically for your events, or write a program that scans the logfile and creates reports that include information from the event-specific data.

In addition to the binary data, you can specify an application-defined category and an application-defined event identifier. The Event Viewer uses the category to filter events written by an event source. The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

Note

The category parameter should be a positive value. Negative category values appear as a complementary positive number in the Event Viewer. For example, a -10 will appear as 65,526, a -1 as 65,535.

To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

Event identifiers, together with the event source, uniquely identify an event. Each application can define its own numbered events and the description strings to which they map. Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

Finally, you can specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

You must create and configure the event source before writing the first entry with the source. Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

See also

Applies to

WriteEntry(String, String, EventLogEntryType, Int32, Int16)

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

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, using the specified registered event source. The category can be used by the Event Viewer to filter events in the log.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type, int eventID, short category);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType * int * int16 -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType, eventID As Integer, category As Short)

Parameters

source
String

The source by which the application is registered on the specified computer.

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

eventID
Int32

The application-specific identifier for the event.

category
Int16

The application-specific subcategory associated with the message.

Exceptions

The source value is an empty string ("").

-or-

The source value is null.

-or-

eventID is less than zero or greater than UInt16.MaxValue.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

int myEventID = 20;
short myCategory = 10;

// Write an informational entry to the event log.
Console::WriteLine( "Write from first source " );
EventLog::WriteEntry( "FirstSource", "Writing warning to event log.",
   EventLogEntryType::Information, myEventID, myCategory );
int myEventID = 20;
short myCategory = 10;
// Write an informational entry to the event log.
Console.WriteLine("Write from first source ");
EventLog.WriteEntry("FirstSource", "Writing warning to event log.",
                     EventLogEntryType.Information, myEventID, myCategory);
Dim myEventID As Integer = 10
Dim myCategory As Short = 20
' Write an informational entry to the event log.
Console.WriteLine("Write from first source ")
EventLog.WriteEntry("FirstSource", "Writing warning to event log.", _
                           EventLogEntryType.Information, myEventID, myCategory)

Remarks

Use this method to write an entry with an application-defined category to the event log, using a source that is already registered as an event source for the appropriate log. The Event Viewer uses the category to filter events written by an event source. The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

Note

The category parameter should be a positive value. Negative category values appear as a complementary positive number in the Event Viewer. For example, a -10 appears as 65,526, a -1 as 65,535.

To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

In addition to the category, you can specify an event identifier for the event being written to the event log. Event identifiers, along with the event source, uniquely identify an event. Each application can define its own numbered events and the description strings to which they map. Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

Finally, you can specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

You must create and configure the event source before writing the first entry with the source. Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

See also

Applies to

WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[])

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

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, and appends binary data to the message.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category, cli::array <System::Byte> ^ rawData);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type, int eventID, short category, byte[] rawData);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType * int * int16 * byte[] -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType, eventID As Integer, category As Short, rawData As Byte())

Parameters

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

eventID
Int32

The application-specific identifier for the event.

category
Int16

The application-specific subcategory associated with the message.

rawData
Byte[]

An array of bytes that holds the binary data associated with the entry.

Exceptions

The Source property of the EventLog has not been set.

-or-

The method attempted to register a new event source, but the computer name in MachineName is not valid.

-or-

The source is already registered for a different event log.

-or-

eventID is less than zero or greater than UInt16.MaxValue.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

// Create the source, if it does not already exist.
String^ myLogName = "myNewLog";
if (  !EventLog::SourceExists( "MySource" ) )
{
   EventLog::CreateEventSource( "MySource", myLogName );
   Console::WriteLine( "Creating EventSource" );
}
else
   myLogName = EventLog::LogNameFromSourceName( "MySource", "." );

// Create an EventLog and assign source.
EventLog^ myEventLog = gcnew EventLog;
myEventLog->Source = "MySource";
myEventLog->Log = myLogName;

// Set the 'description' for the event.
String^ myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType::Warning;
int myApplicatinEventId = 1100;
short myApplicatinCategoryId = 1;

// Set the 'data' for the event.
array<Byte>^ myRawData = gcnew array<Byte>(4);
for ( int i = 0; i < 4; i++ )
{
   myRawData[ i ] = 1;
}
Console::WriteLine( "Writing to EventLog.. " );
myEventLog->WriteEntry( myMessage, myEventLogEntryType, myApplicatinEventId, myApplicatinCategoryId, myRawData );
// Create the source, if it does not already exist.
string myLogName = "myNewLog";
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", myLogName);
   Console.WriteLine("Created EventSource");
   Console.WriteLine("Exiting, execute the application a second time to use the source.");
   return;
}
else
   {
       myLogName = EventLog.LogNameFromSourceName("MySource",".");
   }
   // Create an EventLog and assign source.
   EventLog myEventLog = new EventLog();
myEventLog.Source = "MySource";
myEventLog.Log = myLogName;

// Set the 'description' for the event.
string myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType.Warning;
int myApplicatinEventId = 1100;
short myApplicatinCategoryId = 1;

// Set the 'data' for the event.
byte[] myRawData = new byte[4];
for(int i=0;i<4;i++)
{
   myRawData[i]=1;
}
// Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ");
myEventLog.WriteEntry(myMessage,myEventLogEntryType,
   myApplicatinEventId, myApplicatinCategoryId, myRawData);
' Create the source, if it does not already exist.
dim myLogName as string = "myNewLog"
If Not EventLog.SourceExists("MySource") Then
   EventLog.CreateEventSource("MySource", myLogName)
   Console.WriteLine("Creating EventSource")
else
   myLogName = EventLog.LogNameFromSourceName("MySource",".")
End If

' Create an EventLog and assign source.
Dim myEventLog As New EventLog()
myEventLog.Source = "MySource"
myEventLog.Log = myLogName

' Set the 'description' for the event.
Dim myMessage As String = "This is my event."
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Warning
Dim myApplicatinEventId As Integer = 1100
Dim myApplicatinCategoryId As Short = 1

' Set the 'data' for the event.
Dim myRawData(3) As Byte
Dim i As Integer
For i = 0 To 3
   myRawData(i) = 1
Next i
' Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ")
myEventLog.WriteEntry(myMessage, myEventLogEntryType, myApplicatinEventId, _
                     myApplicatinCategoryId, myRawData)

Remarks

Use this overload to write application-defined event-specific data to the event log. The Event Viewer does not interpret this data; it displays raw data only in a combined hexadecimal and text format. Use event-specific data sparingly, including it only if you are sure it will be useful to someone debugging the problem. You can also use event-specific data to store information the application can process independently of the Event Viewer. For example, you could write a viewer specifically for your events, or write a program that scans the logfile and creates reports that include information from the event-specific data.

In addition to the binary data, you can specify an application-defined category and an application-defined event identifier. The Event Viewer uses the category to filter events written by an event source. The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

Note

The category parameter should be a positive value. Negative category values appear as a complementary positive number in the Event Viewer. For example, a -10 appears as 65,526, a -1 as 65,535.

To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

Event identifiers, along with the event source, uniquely identify an event. Each application can define its own numbered events and the description strings to which they map. Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

Finally, you can specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

You must set the Source property on your EventLog component before you can write entries to the log. You must create and configure the event source before writing the first entry with the source.

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

Note

If you do not specify a MachineName for your EventLog instance before you call CreateEventSource or WriteEntry, the local computer (".") is assumed.

If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

Note

Many exceptions listed above are generated by errors raised during the process of registering the Source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to

WriteEntry(String, String, EventLogEntryType, Int32)

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

Writes an entry with the given message text and application-defined event identifier to the event log, using the specified registered event source.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type, int eventID);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType * int -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType, eventID As Integer)

Parameters

source
String

The source by which the application is registered on the specified computer.

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

eventID
Int32

The application-specific identifier for the event.

Exceptions

The source value is an empty string ("").

-or-

The source value is null.

-or-

eventID is less than zero or greater than UInt16.MaxValue.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

// Create the source, if it does not already exist.
if (  !EventLog::SourceExists( "MySource" ) )
{
   EventLog::CreateEventSource( "MySource", "myNewLog" );
   Console::WriteLine( "Creating EventSource" );
}

// Set the 'description' for the event.
String^ myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType::Warning;
int myApplicationEventId = 100;

// Write the entry in the event log.
Console::WriteLine( "Writing to EventLog.. " );
EventLog::WriteEntry( "MySource", myMessage,
   myEventLogEntryType, myApplicationEventId );
// 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("Creating EventSource");
   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;
}

// Set the 'description' for the event.
string myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType.Warning;
int myApplicationEventId = 100;

// Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ");
EventLog.WriteEntry("MySource",myMessage,
   myEventLogEntryType, myApplicationEventId);
' Create the source, if it does not already exist.
If Not EventLog.SourceExists("MySource") Then
   EventLog.CreateEventSource("MySource", "myNewLog")
   Console.WriteLine("Creating EventSource")
End If

' Set the 'description' for the event.
Dim myMessage As String = "This is my event."
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Warning
Dim myApplicationEventId As Integer = 100

' Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ")
EventLog.WriteEntry("MySource", myMessage, myEventLogEntryType, myApplicationEventId)

Remarks

Use this method to write an entry with an application-defined eventID to the event log, using a source already registered as an event source for the appropriate log. The eventID, along with the source, uniquely identify an event. Each application can define its own numbered events and the description strings to which they map. Event viewers present these strings to the user to help the user understand what went wrong and suggest what actions to take.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

In addition to the event identifier, this overload of WriteEntry lets you specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

You must create and configure the event source before writing the first entry with the source. Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to

WriteEntry(String, EventLogEntryType, Int32, Int16)

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

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type, int eventID, short category);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType * int * int16 -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType, eventID As Integer, category As Short)

Parameters

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

eventID
Int32

The application-specific identifier for the event.

category
Int16

The application-specific subcategory associated with the message.

Exceptions

The Source property of the EventLog has not been set.

-or-

The method attempted to register a new event source, but the computer name in MachineName is not valid.

-or-

The source is already registered for a different event log.

-or-

eventID is less than zero or greater than UInt16.MaxValue.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

// Create an EventLog instance and assign its source.
EventLog^ myLog = gcnew EventLog;
myLog->Source = "ThirdSource";

// Write an informational entry to the event log.
Console::WriteLine( "Write from third source " );
myLog->WriteEntry( "Writing warning to event log.",
   EventLogEntryType::Warning, myEventID, myCategory );
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThirdSource";

// Write an informational entry to the event log.
Console.WriteLine("Write from third source ");
myLog.WriteEntry("Writing warning to event log.",
                  EventLogEntryType.Warning, myEventID, myCategory);
' Create an EventLog instance and assign its source.
Dim myLog As New EventLog()
myLog.Source = "ThirdSource"

' Write an informational entry to the event log.
Console.WriteLine("Write from third source ")
myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning, _
                     myEventID, myCategory)

Remarks

Use this method to write an entry with an application-defined category to the event log. The Event Viewer uses the category to filter events written by an event source. The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

Note

The category parameter should be a positive value. Negative category values appear as a complementary positive number in the Event Viewer. For example, a -10 appears as 65,526, a -1 as 65,535.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

In addition to the category, you can specify an event identifier for the event being written to the event log. Event identifiers, along with the event source, uniquely identify an event. Each application can define its own numbered events and the description strings to which they map. Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

Finally, you can specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

You must set the Source property on your EventLog component before you can write entries to the log. You must create and configure the event source before writing the first entry with the source.

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

Note

If you do not specify a MachineName for your EventLog instance before you call CreateEventSource or WriteEntry, the local computer (".") is assumed.

If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

Note

Many exceptions listed above are generated by errors raised during the process of registering the Source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to

WriteEntry(String, EventLogEntryType)

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

Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType)

Parameters

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

Exceptions

The Source property of the EventLog has not been set.

-or-

The method attempted to register a new event source, but the computer name in MachineName is not valid.

-or-

The source is already registered for a different event log.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

The following example writes a warning entry to an event log, "MyNewLog", on the local computer.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog("MyNewLog");
   myLog->Source = "MyNewLogSource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing warning to event log.", EventLogEntryType::Warning );
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample1
{

    public static void Main()
    {

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog("MyNewLog");
        myLog.Source = "MyNewLogSource";

        // Write an informational entry to the event log.
        myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning);
    }
}
Option Strict
Option Explicit

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog("MyNewLog")
        myLog.Source = "MyNewLogSource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning)
    End Sub
End Class

Remarks

Use this method to write an entry of a specified EventLogEntryType to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

You must set the Source property on your EventLog component before you can write entries to the log. You must create and configure the event source before writing the first entry with the source.

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

Note

If you do not specify a MachineName for your EventLog instance before you call CreateEventSource or WriteEntry, the local computer (".") is assumed.

If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

Note

Many exceptions listed above are generated by errors raised during the process of registering the Source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to

WriteEntry(String, EventLogEntryType, Int32)

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

Writes an entry with the given message text and application-defined event identifier to the event log.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type, int eventID);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType * int -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType, eventID As Integer)

Parameters

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

eventID
Int32

The application-specific identifier for the event.

Exceptions

The Source property of the EventLog has not been set.

-or-

The method attempted to register a new event source, but the computer name in MachineName is not valid.

-or-

The source is already registered for a different event log.

-or-

eventID is less than zero or greater than UInt16.MaxValue.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

// Create an EventLog instance and assign its source.
EventLog^ myLog = gcnew EventLog;
myLog->Source = "ThirdSource";

// Write an informational entry to the event log.
Console::WriteLine( "Write from third source " );
myLog->WriteEntry( "Writing warning to event log.",
   EventLogEntryType::Warning, myEventID, myCategory );
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThirdSource";

// Write an informational entry to the event log.
Console.WriteLine("Write from third source ");
myLog.WriteEntry("Writing warning to event log.",
                  EventLogEntryType.Warning, myEventID, myCategory);
' Create an EventLog instance and assign its source.
Dim myLog As New EventLog()
myLog.Source = "ThirdSource"

' Write an informational entry to the event log.
Console.WriteLine("Write from third source ")
myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning, _
                     myEventID, myCategory)

Remarks

Use this method to write an entry with an application-defined eventID to the event log. The eventID together with the source uniquely identify an event. Each application can define its own numbered events and the description strings to which they map. Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

In addition to the event identifier, you can specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

You must set the Source property on your EventLog component before you can write entries to the log. You must create and configure the event source before writing the first entry with the source.

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

Note

If you do not specify a MachineName for your EventLog instance before you call CreateEventSource or WriteEntry, the local computer (".") is assumed.

If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

Note

Many exceptions listed above are generated by errors raised during the process of registering the Source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to

WriteEntry(String, String)

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

Writes an information type entry with the given message text to the event log, using the specified registered event source.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message);
public static void WriteEntry (string source, string message);
static member WriteEntry : string * string -> unit
Public Shared Sub WriteEntry (source As String, message As String)

Parameters

source
String

The source by which the application is registered on the specified computer.

message
String

The string to write to the event log.

Exceptions

The source value is an empty string ("").

-or-

The source value is null.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

The following example creates the source MySource if it does not already exist, and writes an entry to the event log 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" ) )
   {
      EventLog::CreateEventSource( "MySource", "myNewLog" );
      Console::WriteLine( "CreatingEventSource" );
   }

   
   // Write an informational entry to the event log.    
   EventLog::WriteEntry( "MySource", "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("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;
        }

        // Write an informational entry to the event log.
        EventLog.WriteEntry("MySource", "Writing to event log.");
    }
}
Option Strict
Option Explicit

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") Then
            EventLog.CreateEventSource("MySource", "myNewLog")
            Console.WriteLine("CreatingEventSource")
        End If
        
        
        ' Write an informational entry to the event log.    
        EventLog.WriteEntry("MySource", "Writing to event log.")
    End Sub
End Class

Remarks

Use this method to write an information entry to the event log, using a source that is already registered as an event source for the appropriate log. If you want to specify any other EventLogEntryType, use a different overload of WriteEntry.

You must create and configure the event source before writing the first entry with the source. Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

See also

Applies to

WriteEntry(String)

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

Writes an information type entry, with the given message text, to the event log.

public:
 void WriteEntry(System::String ^ message);
public void WriteEntry (string message);
member this.WriteEntry : string -> unit
Public Sub WriteEntry (message As String)

Parameters

message
String

The string to write to the event log.

Exceptions

The Source property of the EventLog has not been set.

-or-

The method attempted to register a new event source, but the computer name in MachineName is not valid.

-or-

The source is already registered for a different event log.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

The following example creates the source MySource if it does not already exist, and writes an entry to the event log 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

Remarks

Use this method to write an information entry to the event log associated with this EventLog instance. If you want to specify any other EventLogEntryType, use a different overload of WriteEntry.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

You must set the Source property on your EventLog component before you can write entries to the log. You must create and configure the event source before writing the first entry with the source.

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

Note

If you do not specify a MachineName for your EventLog instance before you call CreateEventSource or WriteEntry, the local computer (".") is assumed.

If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

Note

Many of the exceptions listed above are generated by errors raised during the process of registering the Source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to

WriteEntry(String, String, EventLogEntryType)

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

Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log, using the specified registered event source.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType)

Parameters

source
String

The source by which the application is registered on the specified computer.

message
String

The string to write to the event log.

type
EventLogEntryType

One of the EventLogEntryType values.

Exceptions

The source value is an empty string ("").

-or-

The source value is null.

-or-

The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-or-

The source name results in a registry key path longer than 254 characters.

The registry key for the event log could not be opened.

The operating system reported an error when writing the event entry to the event log. A Windows error code is not available.

Examples

The following example writes a warning entry to an event log, "MyNewLog", on the local computer.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Write an informational entry to the event log.    
   EventLog::WriteEntry( "MySource", "Writing warning to event log.", EventLogEntryType::Warning );
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample2
{

    public static void Main()
    {

        // Write an informational entry to the event log.
        EventLog.WriteEntry("MySource",
            "Writing warning to event log.",
            EventLogEntryType.Warning);
    }
}
Option Explicit
Option Strict

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Write an informational entry to the event log.    
        EventLog.WriteEntry("MySource", "Writing warning to event log.", _
            EventLogEntryType.Warning)
    End Sub
End Class

Remarks

Use this method to write an entry of a specified EventLogEntryType to the event log, using a source already registered as an event source for the appropriate log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log.

Note

The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

You must create and configure the event source before writing the first entry with the source. Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. You must have administrative rights on the computer to create a new event source.

The source must be configured either for writing localized entries or for writing direct strings. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. Use the WriteEvent method to write events using a localized message resource file.

If your application writes entries using both resource identifiers and string values, you must register two separate sources. For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

Note

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

See also

Applies to