Export (0) Print
Expand All

EventLog.WriteEntry Method (String, String)

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

Namespace: System.Diagnostics
Assembly: System (in system.dll)

public:
static void WriteEntry (
	String^ source, 
	String^ message
)
public static void WriteEntry (
	String source, 
	String message
)
public static function WriteEntry (
	source : String, 
	message : String
)
Not applicable.

Parameters

source

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

message

The string to write to the event log.

Exception typeCondition

ArgumentException

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

- or -

The source value is a null reference (Nothing in Visual Basic).

- or -

The message string is longer than 32766 bytes.

- or -

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

InvalidOperationException

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

Win32Exception

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

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.

NoteNote:

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.

#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." );
}


import System.*;
import System.Diagnostics.*;
import System.Threading.*;

class MySample
{
    public static void main(String[] args)
    {
        // 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.");
    } //main 
} //MySample

Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft