EventLog.MachineName Property

Gets or sets the name of the computer on which to read or write events.

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

[SettingsBindableAttribute(true)]
public string MachineName { get; set; }

Property Value

Type: System.String
The name of the server on which the event log resides. The default is the local computer (".").

ExceptionCondition
ArgumentException

The computer name is invalid.

If you write to an event log, you must associate a Source with your event log object to connect it to a particular log. It is not necessary to specify the Source property when only reading from a log. You can specify only the Log name and MachineName (server computer name).

NoteNote

You need not specify the MachineName if you are connecting to a log. If you do not specify the MachineName, the local computer (".") is assumed.

A source can only be registered to one log at a time. If the Source property was set for an instance of EventLog, you cannot change the MachineName property for that EventLog without changing the value of Source or calling DeleteEventSource first. If you change the MachineName property, the EventLog closes all handles and reattaches to the log and source on the new computer.

The MachineName value cannot be an empty string. If it is not explicitly set, it defaults to the local computer (".").

The following example reads entries in the event log, "NewEventLog", on a specified computer.

using System;
using System.Diagnostics;

class MySample{

    public static void Main(){

        EventLog myNewLog = new EventLog();
        myNewLog.Log = "NewEventLog";
        myNewLog.MachineName = "MyServer";                      
        foreach(EventLogEntry entry in myNewLog.Entries){
            Console.WriteLine("\tEntry: " + entry.Message);
        }    
    }       
}
   

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft