EventLog.WriteEntry Method (String)
Writes an information type entry, with the given message text, to the event log.
Assembly: System (in System.dll)
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.
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.
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.
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.
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.
If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.
The following example creates the source MySource if it does not already exist, and writes an entry to the event log MyNewLog.
Option Explicit Option Strict Imports System 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 'Main End Class 'MySample
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.