共用方式為


EventLog.Source 屬性

定義

在寫入事件日誌時,取得或設定原始碼名稱註冊並使用。

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

屬性值

該名稱登記在事件日誌中作為條目來源。 預設為空字串(“”)。

屬性

例外狀況

來源名稱會產生超過254字元的登錄金鑰路徑。

範例

以下範例若來源尚未存在,則建立該來源 MySource ,並將條目寫入事件日誌 MyNewLog

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

備註

事件來源會顯示什麼會記錄該事件。 它通常是應用程式名稱,或若應用程式規模較大,則是其子元件名稱。 應用程式與服務應寫入應用程式日誌或自訂日誌。 裝置驅動程式應該寫入系統日誌。

只有在寫入事件日誌時,才需要指定事件來源。 在撰寫事件日誌前,您必須將事件來源註冊為有效的事件來源。 當你撰寫日誌條目時,系統會利用該 Source 屬性來尋找合適的日誌來放置你的條目。 如果你正在閱讀事件日誌,你可以指定 Source、 或 a LogMachineName

備註

如果你是連接到本地電腦的日誌,則不需要特別指定。MachineName 若未指定 MachineName,則假設為本地電腦(「.」)。

使用 WriteEventWriteEntry 撰寫事件到事件日誌。 您必須指定事件來源來撰寫事件;您必須先建立並設定事件來源,才能撰寫第一個包含原始碼的條目。

在安裝應用程式時建立新的事件來源。 這讓作業系統有時間更新註冊事件來源及其設定。 如果作業系統沒有重新整理事件來源清單,而你嘗試用新來源寫入事件,寫入操作將會失敗。 你可以用 EventLogInstaller,或是用 的方法 CreateEventSource 來設定新的來源。 您必須在電腦上擁有管理權限才能建立新的事件來源。

你可以為現有事件日誌或新的事件日誌建立事件來源。 當你為新事件日誌建立新來源時,系統會註冊該日誌的來源,但直到第一個紀錄被寫入後,日誌才會被建立。

來源必須在本地電腦上是唯一的;新的來源名稱無法與現有的來源名稱或現有的事件日誌名稱相匹配。 每個來源一次只能寫入一個事件日誌;不過,你的應用程式可以使用多個來源來寫入多個事件日誌。 例如,你的應用程式可能需要為不同的事件日誌或不同的資源檔案設定多個來源。

如果你更改了值, Source 該值所註冊的會 EventLog 關閉,所有事件帳柄也會被釋放。

原始碼必須設定用於寫入本地化條目或直接字串。 如果你的應用程式同時使用資源識別碼和字串值來寫入條目,你必須註冊兩個獨立的來源。 例如,設定一個來源的資源檔案,然後在該 WriteEvent 方法中使用該來源,將資源識別碼寫入事件日誌。 接著建立一個沒有資源檔案的來源,並在方法中使用該來源 WriteEntry 直接將字串寫入事件日誌。

要更改現有來源的設定細節,必須先刪除該來源,然後用新的設定建立它。 如果其他應用程式或元件使用現有原始碼,請建立一個包含更新設定的新原始碼,而非刪除現有原始碼。

備註

如果一個來源已經被映射到日誌,而你又重新映射到新的日誌,必須重新啟動電腦才能生效變更。

適用於

另請參閱