Microsoft.Extensions.Logging.EventSource
A logger that writes messages to EventSource instance.
On Windows platforms EventSource will deliver messages using Event Tracing for Windows (ETW) events.
On Linux EventSource will use LTTng (http://lttng.org) to deliver messages.
ActivityScope is just a IDisposable that knows how to send the ActivityStop event when it is
desposed. It is part of the BeginScope() support.
'serializes' a given exception into an ExceptionInfo (that EventSource knows how to serialize)
ExceptionInfo object represending a .NET Exception
ETW does not support a concept of a null value. So we use an un-initialized object if there is no exception in the event data.
Converts an ILogger state object into a set of key-value pairs (That can be send to a EventSource)
The provider for the .
Represents information about exceptions that is captured by EventSourceLogger
The LoggingEventSource is the bridge form all ILogger based logging to EventSource/EventListener logging.
You turn this logging on by enabling the EvenSource called
Microsoft-Extensions-Logging
When you enabled the EventSource, the EventLevel you set is translated in the obvious way to the level
associated with the ILogger (thus Debug = verbose, Informational = Informational ... Critical == Critical)
This allows you to filter by event level in a straighforward way.
For finer control you can specify a EventSource Argument called
FilterSpecs
The FilterSpecs argument is a semicolon separated list of specifications. Where each specification is
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).
Where Name is the name of a ILoggger (case matters), Name can have a * which acts as a wildcard
AS A SUFFIX. Thus Net* will match any loggers that start with the 'Net'.
The LEVEL is a number or a LogLevel string. 0=Trace, 1=Debug, 2=Information, 3=Warning, 4=Error, Critical=5
This speicifies the level for the associated pattern. If the number is not specified, (first form
of the specification) it is the default level for the EventSource.
First match is used if a partciular name matches more than one pattern.
In addition the level and FilterSpec argument, you can also set EventSource Keywords. See the Keywords
definition below, but basically you get to decide if you wish to have
* Keywords.Message - You get the event with the data in parsed form.
* Keywords.JsonMessage - you get an event with the data in parse form but as a JSON blob (not broken up by argument ...)
* Keywords.FormattedMessage - you get an event with the data formatted as a string
It is expected that you will turn only one of these keywords on at a time, but you can turn them all on (and get
the same data logged three different ways.
Example Usage
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 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]);
}
}
This is public from an EventSource consumer point of view, but since these defintions
are not needed outside this class
Meta events are evnets about the LoggingEventSource itself (that is they did not come from ILogger
Turns on the 'Message' event when ILogger.Log() is called. It gives the information in a programatic (not formatted) way
Turns on the 'FormatMessage' event when ILogger.Log() is called. It gives the formatted string version of the information.
Turns on the 'MessageJson' event when ILogger.Log() is called. It gives JSON representation of the Arguments.
The one and only instance of the LoggingEventSource.
FormattedMessage() is called when ILogger.Log() is called. and the FormattedMessage keyword is active
This only gives you the human reasable formatted message.
Message() is called when ILogger.Log() is called. and the Message keyword is active
This gives you the logged information in a programatic format (arguments are key-value pairs)
ActivityStart is called when ILogger.BeginScope() is called
Set the filtering specifcation. null means turn off all loggers. Empty string is turn on all providers.
Given a set of specifications Pat1:Level1;Pat1;Level2 ... Where
Pat is a string pattern (a logger Name with a optional trailing wildcard * char)
and Level is a number 0 (Trace) through 5 (Critical).
The :Level can be omitted (thus Pat1;Pat2 ...) in which case the level is 1 (Debug).
A completely emtry sting act like * (all loggers set to Debug level).
The first specification that 'loggers' Name matches is used.
Parses the level specification (which should look like :N where n is a number 0 (Trace)
through 5 (Critical). It can also be an empty string (which means 1 (Debug) and ';' marks
the end of the specifcation This specification should start at spec[curPos]
It returns the value in 'ret' and returns true if successful. If false is returned ret is left unchanged.
Extension methods for the class.
Adds an event logger named 'EventSource' to the factory.
The extension method argument.
Adds an event logger that is enabled for .Information or higher.
The extension method argument.