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 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 {eventData.Payload[2]}: {eventData.Payload[4]}");
    }
}

注釈

このログ記録を有効にするには、Microsoft-Extensions-Logging という EventSource を有効にします。 EventSource を有効にすると、設定した EventLevel は ILogger に関連付けられているレベル ( Debug = verboseInformational = InformationalCritical == Critical) に変換されます。 これにより、簡単な方法でイベント レベルでフィルター処理できます。

より細かく制御するには、 FilterSpecsと呼ばれる EventSource 引数を指定します。 FilterSpecs引数は、セミコロンで区切られた仕様の一覧です。 各仕様は次のとおりです。

SPEC = // *と同じ空の仕様。

|NAME // 名前付き仕様。既定のレベルを使用します。

|NAME : LEVEL // 特定のロガーのレベルを指定します (* サフィックスを付けることができます)。

FilterSpecsで "UseAppFilters" を指定すると、すべてのカテゴリを無効にすることが回避されます。既定では無効になります。

NameILogger の名前 (大文字と小文字) で、ワイルドカードとして機能する *で終わる場合があります。 たとえば、 Net* は 'Net' で始まるロガーと一致します。

LEVEL は、数値または LogLevel 文字列です (0=Trace、1=Debug、2=Information、3=Warning、4=Error、Critical=5)。 これにより、関連付けられているパターンのレベルが指定されます。 数値が指定されていない場合 (仕様の最初の形式)、EventSource の既定のレベルになります。

最初の一致は、特定の名前が複数のパターンと一致する場合に使用されます。

level 引数と FilterSpecs 引数に加えて、EventSource キーワードを設定することもできます。

* Keywords.Message - イベントには、解析された形式のデータが含まれています。

* Keywords.JsonMessage - イベントには、解析された形式のデータが含まれていますが、JSON BLOB として格納されます (引数によって分割されません)。

* Keywords.FormattedMessage - イベントには、文字列として書式設定されたデータが含まれています。

一度に有効にできるのはこれらのキーワードの 1 つだけですが、すべて有効にすることができます (同じデータを 3 つの異なる方法でログに記録できます)。

適用対象