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 = verbose: ,InformationalCriticalInformational = Critical == )와 연결된 수준으로 변환됩니다. 이렇게 하면 이벤트 수준별로 간단하게 필터링할 수 있습니다.

더 세부적인 제어를 위해 ..라는 FilterSpecsEventSource 인수를 지정할 수 있습니다. 인수는 FilterSpecs 세미콜론으로 구분된 사양 목록입니다. 각 사양은 다음과 같습니다.

SPEC = // 빈 사양, *와 동일합니다.

| NAME // Named spec. 기본 수준을 사용합니다.

| NAME : LEVEL // 특정 로거에 대한 수준을 지정합니다(* 접미사가 있을 수 있습니다).

"UseAppFilters"가 지정되면 FilterSpecs모든 범주를 사용하지 않도록 설정하지 않습니다. 그렇지 않으면 기본적으로 발생합니다.

Name 는 (대/소문자 ILogger )의 이름이며 와일드카드 역할을 하는 *로 끝날 수 있습니다. 예를 들어 Net* 'Net'으로 시작하는 모든 로거와 일치합니다.

LEVEL 은 숫자 또는 LogLevel 문자열입니다(0=Trace, 1=Debug, 2=Information, 3=Warning, 4=Error, Critical=5). 연결된 패턴의 수준을 지정합니다. 숫자가 지정되지 않은 경우(사양의 첫 번째 형태) EventSource의 기본 수준입니다.

특정 이름이 둘 이상의 패턴과 일치하는 경우 첫 번째 일치 항목이 사용됩니다.

수준 및 FilterSpecs 인수 외에도 EventSource 키워드를 설정할 수도 있습니다.

* Keywords.Message - 이벤트에 구문 분석된 형식의 데이터가 포함됩니다.

* Keywords.JsonMessage - 이벤트는 구문 분석된 형식이지만 JSON Blob으로 데이터를 포함합니다(인수로 분할되지 않음).

* Keywords.FormattedMessage - 이벤트에는 문자열 형식의 데이터가 포함됩니다.

이러한 키워드 중 하나만 한 번에 켜져 있지만 모두 켜고 동일한 데이터를 세 가지 방법으로 기록할 수 있습니다.

적용 대상