LoggingEventSource 類別

定義

LoggingEventSource 是所有 ILogger 式記錄與 EventSource/EventListener 記錄間的橋梁。

public ref class LoggingEventSource sealed : System::Diagnostics::Tracing::EventSource
[System.Diagnostics.Tracing.EventSource(Name="Microsoft-Extensions-Logging")]
public sealed class LoggingEventSource : System.Diagnostics.Tracing.EventSource
[<System.Diagnostics.Tracing.EventSource(Name="Microsoft-Extensions-Logging")>]
type LoggingEventSource = class
    inherit EventSource
Public NotInheritable Class LoggingEventSource
Inherits EventSource
繼承
LoggingEventSource
屬性

範例

下列範例示範如何使用 EventListener 來取得 ILogging 資訊:

class MyEventListener : EventListener 
{
    protected override void OnEventSourceCreated(EventSource eventSource) 
    {
        if (eventSource.Name == "Microsoft-Extensions-Logging") 
        {
            // initialize a string, string dictionary of arguments to pass to the EventSource.
            // Turn on loggers matching App* to Information, everything else (*) is the default level (which is EventLevel.Error)
            var args = new Dictionary<string, string>() { { "FilterSpecs", "App*:Information;*" } };
            // Set the default level (verbosity) to Error, and only ask for the formatted messages in this case.
            EnableEvents(eventSource, EventLevel.Error, LoggingEventSource.Keywords.FormattedMessage, args);
        }
    }

    protected override void OnEventWritten(EventWrittenEventArgs eventData) 
    {
        // Look for the formatted message event, which has the following argument layout (as defined in the LoggingEventSource.
        // FormattedMessage(LogLevel Level, int FactoryID, string LoggerName, string EventId, string FormattedMessage);
        if (eventData.EventName == "FormattedMessage")
            Console.WriteLine("Logger {0}: {1}", eventData.Payload[2], eventData.Payload[4]);
    }
}

備註

您可以啟用名為 Microsoft-Extensions-Logging 的 EventSource 來開啟此記錄。

當您啟用 EventSource 時,您設定的 EventLevel 會以明顯的方式轉譯為與 ILogger 相關聯的層級 (因此 Debug = verbose、Informational = Informational ...Critical == Critical)

這可以讓您以直接的方式篩選事件層級。

如需更精細的控制,您可以指定名為 的 FilterSpecs EventSource 引數。

FilterSpecs 變數是以分號分隔的規格清單。 其中每個規格都是

SPEC =                          // empty spec, same as *
     | NAME                     // Just a name the level is the default level
     | NAME : LEVEL            // specifies level for a particular logger (can have a * suffix).

其中 Name 是 ILogger (大小寫的名稱很重要) ,Name 可以有 * 作為萬用字元 AS A SUFFIX。 因此,Net* 會比對任何以 'Net' 開頭的記錄器。

LEVEL 是數字或 LogLevel 字串。 0=Trace、1=Debug、2=Information、3=Warning、4=Error、Critical=5 這會指定相關聯模式的層級。 若沒有指定數字 (規格的第一種形式),則為 EventSource 的預設層級。

若與特定名稱相符的模式超過一種,則會使用第一個相符項目。

除了 level 和 FilterSpec 引數之外,您也可以設定 EventSource 關鍵字。 請參閱以下的關鍵字定義,但基本上您必須決定您想要的是:

  • Keywords.Message - 您會以剖析形式取得具有資料的事件。
  • Keywords.JsonMessage - 您會以剖析形式取得具有資料的事件,但 JSON Blob (未依引數分割 ...)
  • Keywords.FormattedMessage - 您會取得資料格式化為字串的事件

預期您一次只會開啟其中一個關鍵字,但您可以全部開啟,並取得記錄三種不同方式的相同資料。

屬性

ConstructionException

取得事件來源建構期間擲回的任何例外狀況。

(繼承來源 EventSource)
Guid

事件來源的唯一識別項。

(繼承來源 EventSource)
Name

衍生自事件來源的類別的好記名稱。

(繼承來源 EventSource)
Settings

取得套用至這個事件來源的設定。

(繼承來源 EventSource)

方法

Dispose()

釋放 EventSource 類別目前的執行個體所使用的全部資源。

(繼承來源 EventSource)
Dispose(Boolean)

釋放 EventSource 類別所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 EventSource)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetTrait(String)

取得與指定索引鍵關聯的特性值。

(繼承來源 EventSource)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsEnabled()

判斷是否已啟用目前的事件來源。

(繼承來源 EventSource)
IsEnabled(EventLevel, EventKeywords)

判斷是否已啟用具有指定之層級和關鍵字的目前事件來源。

(繼承來源 EventSource)
IsEnabled(EventLevel, EventKeywords, EventChannel)

判斷具有指定的層級、關鍵字和通道的事件是否已啟用目前的事件來源。

(繼承來源 EventSource)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnEventCommand(EventCommandEventArgs)

當控制器更新目前事件來源時呼叫。

(繼承來源 EventSource)
ToString()

取得目前事件來源執行個體的字串表示。

(繼承來源 EventSource)
Write(String)

寫入不含欄位,但具有指定名稱和預設選項的事件。

(繼承來源 EventSource)
Write(String, EventSourceOptions)

寫入不含欄位,但具有指定名稱和選項的事件。

(繼承來源 EventSource)
Write<T>(String, EventSourceOptions, Guid, Guid, T)

寫入具有指定名稱、選項、相關活動和事件資料的事件。

(繼承來源 EventSource)
Write<T>(String, EventSourceOptions, T)

寫入具有指定名稱、事件資料和選項的事件。

(繼承來源 EventSource)
Write<T>(String, EventSourceOptions, T)

寫入具有指定名稱、選項和事件資料的事件。

(繼承來源 EventSource)
Write<T>(String, T)

寫入具有指定名稱和資料的事件。

(繼承來源 EventSource)
WriteEvent(Int32)

使用所提供的事件識別元,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Byte[])

使用所提供的事件識別項和位元組陣列引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, EventSource+EventSourcePrimitive[])

使用提供的事件識別碼和可變數目的事件來源基本類型來寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int32)

使用所提供的事件識別元和 32 位元整數引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int32, Int32)

使用所提供的事件識別元和 32 位元整數引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int32, Int32, Int32)

使用所提供的事件識別元和 32 位元整數引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int32, String)

使用所提供的事件識別項、32 位元整數和字串引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int64)

使用所提供的事件識別元和 64 位元整數引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int64, Byte[])

使用指定的識別項、64 位元整數和位元組陣列引數,寫入事件資料。

(繼承來源 EventSource)
WriteEvent(Int32, Int64, Int64)

使用所提供的事件識別元和 64 位元整數引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int64, Int64, Int64)

使用所提供的事件識別元和 64 位元整數引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Int64, String)

使用所提供的事件識別項、64 位元整數和字串引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, Object[])

使用所提供的事件識別元和引數陣列,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, String)

使用所提供的事件識別元和字串引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, String, Int32)

使用所提供的事件識別元和引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, String, Int32, Int32)

使用所提供的事件識別元和引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, String, Int64)

使用所提供的事件識別元和引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, String, String)

使用所提供的事件識別元和字串引數,寫入事件。

(繼承來源 EventSource)
WriteEvent(Int32, String, String, String)

使用所提供的事件識別元和字串引數,寫入事件。

(繼承來源 EventSource)
WriteEventCore(Int32, Int32, EventSource+EventData*)

使用所提供的事件識別項和事件資料,建立新的 WriteEvent 多載。

(繼承來源 EventSource)
WriteEventWithRelatedActivityId(Int32, Guid, Object[])

寫入表示目前活動與另一個活動有關的事件。

(繼承來源 EventSource)
WriteEventWithRelatedActivityIdCore(Int32, Guid*, Int32, EventSource+EventData*)

寫入表示目前活動與另一個活動有關的事件。

(繼承來源 EventSource)

事件

EventCommandExecuted

發生於命令來自事件接聽程式時。

(繼承來源 EventSource)

適用於