ETWLib uses two primary abstractions to manage event streams:
Traces
A Trace represents a stream of events. ETWLib distinguishes between Kernel traces and User traces.
- UserTrace: Represents events from ETW-aware applications. You can create an unnamed trace or provide a specific name.
- Kernel traces: (Note: Kernel trace implementation is currently not supported in ETWLib).
Providers
A Provider represents a specific source of ETW events, identified by a unique GUID. Providers use bitflags for event filtering:
Any: If an event meets any of the flags in this property, registered callbacks are triggered.All: If an event meets all the bits in this property, registered callbacks are triggered.
Note: The exact semantics of Any and All depend on the specific ETW provider being used; some providers may ignore the All flag if Any is not set.
To receive events, you must:
- Create a
UserTrace. - Create a
Provider with its GUID. - Configure the provider's
Any or All bitflags. - Register a callback to the provider's
OnEvent event. - Enable the provider on the trace using
trace.Enable(provider). - Start the trace using
trace.Start().
// 1. Create a trace
UserTrace namedTrace = new UserTrace("Muffins McGoo");
// 2. Define a provider with a GUID
Provider powershellProvider = new Provider(Guid.Parse("{A0C1853B-5C40-4B15-8766-3CF1C58F985A}"));
// 3. Set filtering flags and register a callback
powershellProvider.Any = 0x10;
powershellProvider.OnEvent += MyCallbackFunction;
// 4. Enable the provider on the trace
namedTrace.Enable(powershellProvider);
// 5. Start the trace (blocking call)
// Use Task.Run to prevent blocking the main thread
var t = Task.Run(() => namedTrace.Start());
void MyCallbackFunction(EventRecord record)
{
// Handle the event
}