EventLog.Source 屬性

定義

取得或設定寫入至事件記錄檔時登錄和使用的來源名稱。Gets or sets the source name to register and use when writing to the event log.

public:
 property System::String ^ Source { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.SettingsBindable(true)]
public string Source { get; set; }
member this.Source : string with get, set
Public Property Source As String

屬性值

這個名稱 (具有事件記錄檔),被登錄為項目的來源。The name registered with the event log as a source of entries. 預設為空字串 ("")。The default is an empty string ("").

屬性

例外狀況

來源名稱產生的登錄機碼路徑長度超過 254 個字元。The source name results in a registry key path longer than 254 characters.

範例

下列範例會建立來源 MySource (如果尚未存在),並將專案寫入事件記錄檔 MyNewLogThe 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" );
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog;
   myLog->Source = "MySource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing to event log." );
   Console::WriteLine( "Message written to event log." );
}

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"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }
                
        // 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.");
        
        Console.WriteLine("Message written to event log.");                                                                        
    }
}
   
Option Explicit
Option Strict
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.")
        
        Console.WriteLine("Message written to event log.")
    End Sub
End Class

備註

事件來源會指出記錄事件的原因。The event source indicates what logs the event. 這通常是應用程式的名稱,或是應用程式的子元件名稱(如果應用程式很大)。It is often the name of the application, or the name of a subcomponent of the application, if the application is large. 應用程式和服務應寫入應用程式記錄檔或自訂記錄檔。Applications and services should write to the Application log or a custom log. 設備磁碟機應該寫入系統記錄檔。Device drivers should write to the System log.

如果您要寫入事件記錄檔,您只需要指定事件來源。You only need to specify an event source if you are writing to an event log. 將專案寫入事件記錄檔之前,您必須以事件記錄檔註冊事件來源,做為事件的有效來源。Before writing an entry to an event log, you must register the event source with the event log as a valid source of events. 當您撰寫記錄專案時,系統會使用 Source 屬性來尋找適當的記錄檔,以放置您的專案。When you write a log entry, the system uses the Source property to find the appropriate log in which to place your entry. 如果您要讀取事件記錄檔,您可以指定 SourceLogMachineNameIf you are reading the event log, you can either specify the Source, or a Log and MachineName.

注意

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

使用 WriteEventWriteEntry,將事件寫入事件記錄檔。Use WriteEvent and WriteEntry to write events to an event log. 您必須指定要寫入事件的事件來源;在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。You must specify an event source to write events; 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. 您可以使用 EventLogInstaller或使用 CreateEventSource 方法來設定新的來源。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.

您可以為現有的事件記錄檔或新的事件記錄檔建立事件來源。You can create an event source for an existing event log or a new event log. 當您建立新事件記錄檔的新來源時,系統會註冊該記錄檔的來源,但在寫入第一個專案之前,不會建立記錄檔。When you create a new source for a new event log, the system registers the source for that log, but the log is not created until the first entry is written to it.

來源在本機電腦上必須是唯一的;新的來源名稱不能符合現有的來源名稱或現有的事件記錄檔名稱。The source must be unique on the local computer; a new source name cannot match an existing source name or an existing event log name. 每個來源一次只能寫入一個事件記錄檔;不過,您的應用程式可以使用多個來源寫入多個事件記錄檔。Each source can write to only one event log at a time; however, your application can use multiple sources to write to multiple event logs. 例如,您的應用程式可能需要針對不同的事件記錄檔或不同的資源檔設定多個來源。For example, your application might require multiple sources configured for different event logs or different resource files.

如果您變更 Source 值,則會將它所註冊的 EventLog 關閉,並釋放所有事件控制碼。If you change the Source value, the EventLog to which it is registered is closed and all event handles are released.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。If your application writes entries using both resource identifiers and string values, you must register two separate sources. 例如,設定一個具有資源檔的來源,然後在 WriteEvent 方法中使用該來源,將使用資源識別碼的專案寫入事件記錄檔。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. 然後建立不含資源檔的不同來源,並在 WriteEntry 方法中使用該來源,將字串直接寫入至使用該來源的事件記錄檔。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.

若要變更現有來源的設定詳細資料,您必須刪除來源,然後使用新的設定加以建立。To change the configuration details of an existing source, you must delete the source and then create it with the new configuration. 如果其他應用程式或元件使用現有的來源,請使用更新的設定來建立新的來源,而不是刪除現有的來源。If other applications or components use the existing source, create a new source with the updated configuration rather than deleting the existing source.

注意

如果來源已經對應到記錄檔,而且您將它重新對應到新的記錄檔,您必須重新開機電腦,變更才會生效。If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

安全性

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

適用於

另請參閱