Export (0) Print
Expand All

EventLog.WriteEntry Method (String)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Sub WriteEntry( _
   ByVal message As String _
)
[C#]
public void WriteEntry(
 string message
);
[C++]
public: void WriteEntry(
 String* message
);
[JScript]
public function WriteEntry(
   message : String
);

Parameters

message
The string to write to the event log.

Exceptions

Exception Type Condition
ArgumentException The Source property of the EventLog has not been set.

-or-

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

-or-

The method attempted to create a new event source, but the source name was not specified.

-or-

The method attempted to create a new event source, but the source already exists on the computer.

-or-

The method attempted to create a new log, but the first eight characters of the log name are not unique on the computer.

-or-

The method attempted to open the event log with read access, but the Log property was not specified.

-or-

The method attempted to open the event log with write access, but the Source property was not specified.

-or-

The message string is too long. The size must be less than 16384 bytes.

-or-

The source is not registered to the given log.

Exception The registry entry for the log could not be opened on a remote computer.
InvalidOperationException The method attempted to open the event log with write access, but you do not have write access to the log.
Win32Exception The method attempted to release the event log's read or write handle but failed.

-or-

The method attempted to open the event log with read access but failed.

-or-

The event could not be reported to the log.

SystemException The event log could not be notified to start receiving events.

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.

You must set the Source property on your EventLog component before you can write entries to the log. You can call CreateEventSource on a new source to register it before you write to the event log, but this is not necessary. If the source specified in the Source property of this EventLog instance is not registered on the computer 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, it defaults to the Application log.

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

WriteEntry lets you specify a string message to write to the log. This method writes the given string directly to the log; it does not use a localizable message file.

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.

Example

[Visual Basic, C#, C++] The following example writes an entry to an event log, "MyNewLog", on the local computer, and creates the source "MySource" if the source does not already exist.

[Visual Basic, C#, C++] Note   It is not necessary for you to create the event source in your code. If the source you set in the Source property does not already exist, the WriteEntry method creates it before writing to the event log.
[Visual Basic] 
Option Explicit
Option Strict

Imports System
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
        
        ' 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

[C#] 
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")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
        }
                
        // 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.");
        
    }
}
   

[C++] 
#using <mscorlib.dll>
#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(S"MySource")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog");
        Console::WriteLine(S"CreatingEventSource");
    }

    // Create an EventLog instance and assign its source.
    EventLog* myLog = new EventLog();
    myLog->Source = S"MySource";

    // Write an informational entry to the event log.    
    myLog->WriteEntry(S"Writing to event log.");

}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.WriteEntry Overload List | Source | CreateEventSource | DeleteEventSource | SourceExists | Entries

Show:
© 2014 Microsoft