EventLog.MachineName 屬性


取得或設定要在其上讀取或寫入事件的電腦的名稱。Gets or sets the name of the computer on which to read or write events.

 property System::String ^ MachineName { System::String ^ get(); void set(System::String ^ value); };
public string MachineName { get; set; }
public string MachineName { get; set; }
member this.MachineName : string with get, set
Public Property MachineName As String


事件記錄檔所在處的伺服器名稱。The name of the server on which the event log resides. 預設值是本機電腦 (".")。The default is the local computer (".").



電腦名稱無效。The computer name is invalid.


下列範例會在指定的電腦上讀取事件記錄檔 "NewEventLog" 中的專案。The following example reads entries in the event log, "NewEventLog", on a specified computer.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
int main()
   EventLog^ myNewLog = gcnew EventLog;
   myNewLog->Log = "NewEventLog";
   myNewLog->MachineName = "MyServer";
   System::Collections::IEnumerator^ myEnum = myNewLog->Entries->GetEnumerator();
   while ( myEnum->MoveNext() )
      EventLogEntry^ entry = safe_cast<EventLogEntry^>(myEnum->Current);
      Console::WriteLine( "\tEntry: {0}", entry->Message );

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);
Imports System.Diagnostics

Class MySample
    Public Shared Sub Main()
        Dim myNewLog As New EventLog()
        myNewLog.Log = "NewEventLog"
        myNewLog.MachineName = "MyServer"
        Dim entry As EventLogEntry
        For Each entry In  myNewLog.Entries
            Console.WriteLine((ControlChars.Tab & "Entry: " & entry.Message))
        Next entry
    End Sub
End Class


如果您寫入事件記錄檔,您必須將 Source 與事件記錄檔物件產生關聯,以將它連接到特定的記錄檔。If you write to an event log, you must associate a Source with your event log object to connect it to a particular log. 只有在記錄檔中讀取時,才需要指定 Source 屬性。It is not necessary to specify the Source property when only reading from a log. 您只能指定 Log 名稱和 MachineName (伺服器電腦名稱稱)。You can specify only the Log name and MachineName (server computer name).


如果您要連接到記錄檔,則不需要指定 MachineNameYou need not specify the MachineName if you are connecting to a log. 如果您未指定 MachineName,則會假設為本機電腦(".")。If you do not specify the MachineName, the local computer (".") is assumed.

來源一次只能註冊至一個記錄檔。A source can only be registered to one log at a time. 如果 Source 屬性是針對 EventLog的實例所設定,您就無法變更該 EventLogMachineName 屬性,而不需要變更 Source 或先呼叫 DeleteEventSource 的值。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. 如果您變更 [MachineName] 屬性,EventLog 就會關閉所有控制碼,並會重新連結到新電腦上的記錄檔和來源。If you change the MachineName property, the EventLog closes all handles and reattaches to the log and source on the new computer.

MachineName 值不可以是空字串。The MachineName value cannot be an empty string. 如果未明確設定,則會預設為本機電腦(".")。If it is not explicitly set, it defaults to the local computer (".").


用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉: WriteAssociated enumeration: Write