EventLog.MachineName EventLog.MachineName EventLog.MachineName EventLog.MachineName Property

定義

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

public:
 property System::String ^ MachineName { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.SettingsBindable(true)]
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
Imports System.Diagnostics
Imports Microsoft.VisualBasic

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 ' Main
End Class ' MySample

備註

如果您寫入事件記錄檔時,就必須將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).

注意

您不需要指定MachineName如果您要連接到記錄檔。You 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,您無法變更MachineName屬性,該EventLog而不需要變更的值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 (".").

安全性

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

適用於

另請參閱