Condividi tramite


LoggingEventSource Class

Definition

Bridges all ILogger-based logging to EventSource/EventListener logging.

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
Inheritance
LoggingEventSource
Attributes

Examples

This example shows how to use an EventListener to get ILogging information.

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]}");
    }
}

Remarks

To turn this logging on, you enable the EventSource called Microsoft-Extensions-Logging. When you enable the EventSource, the EventLevel you set is translated in to the level associated with the ILogger (thus Debug = verbose, Informational = Informational, Critical == Critical). This allows you to filter by event level in a straightforward way.

For finer control, you can specify an EventSource argument called FilterSpecs. The FilterSpecs argument is a semicolon-separated list of specifications. Where each specification is:

SPEC = // Empty spec, same as *.

| NAME // Named spec. Uses the default level.

| NAME : LEVEL // Specifies level for a particular logger (can have a * suffix).

When "UseAppFilters" is specified in the FilterSpecs, it avoids disabling all categories, which happens by default otherwise.

Name is the name of an ILogger (case matters), and can end with *, which acts as a wildcard. For example, Net* will match any loggers that start with 'Net'.

LEVEL is a number or a LogLevel string (0=Trace, 1=Debug, 2=Information, 3=Warning, 4=Error, Critical=5). This specifies the level for the associated pattern. If the number isn't specified (first form of the specification), it's the default level for the EventSource.

The first match is used if a particular name matches more than one pattern.

In addition to the level and FilterSpecs argument, you can also set EventSource keywords:

* Keywords.Message - Event contains data in parsed form.

* Keywords.JsonMessage - Event contains data in parsed form but as a JSON blob (not broken up by argument).

* Keywords.FormattedMessage - Event contains data formatted as a string.

It's expected that only one of these keywords is turned on at a time, but you can turn them all on (and get the same data logged three different ways).

Applies to