TraceLogging for Rust
The tracelogging crate provides a simple and efficient system for
logging TraceLogging events when the event schema is known at compile time.
This is similar to the C/C++ TraceLoggingProvider.h implementation in the Windows SDK.
use tracelogging as tlg;
// Define a static variable for the "MyCompany.MyComponent" provider.
// Note that provider variable is not pub so it is not visible outside the
// module. To share the variable with multiple modules, put the define_provider
// in the parent module, e.g. in lib.rs.
define_provider!; // The provider's name (string literal).
// Register the provider at module initialization. If you don't register (or if
// register fails) then MY_PROVIDER.enabled() will always return false, the
// write_event macro will be a no-op, and MY_PROVIDER.unregister() will be a no-op.
// Safety: MUST call MY_PROVIDER.unregister() before module unload.
unsafe
// As necessary, call write_event to send events to ETW.
let field1_value = "String Value";
let field2_value = 42u32;
write_event!;
// Before module unload, unregister the provider.
MY_PROVIDER.unregister;
Configuration
This crate supports the following configurable features:
etw: Use Windows ETW APIs to log events. If not enabled, all logging operations will be no-ops. Enabled by default.kernel_mode: Use kernel-mode ETW APIs (e.g.EtwWriteTransfer) instead of user-mode ETW APIs (e.g.EventWriteTransfer).macros: Re-export thedefine_provider!andwrite_event!macros from thetracelogging_macroscrate. Enabled by default.
In addition, this crate will log events only if compiled for a Windows operating system. If compiled for a non-Windows operating system, all logging operations will be no-ops.