This article shows how to use PostSharp Logging and Event Tracing for Windows (ETW) together.
Targeting ETW
To target ETW with PostSharp Logging:
Add PostSharp Logging to your codebase as described in Getting Started with PostSharp Logging.
Add the
PostSharp.Patterns.Diagnostics.Tracingpackage to your startup project.In the application startup file, include the following namespace imports:
using PostSharp.Patterns.Diagnostics; using PostSharp.Patterns.Diagnostics.Backends.EventSource;In the application startup method, include the following code:
var eventSourceBackend = new EventSourceLoggingBackend(new PostSharpEventSource()); if (eventSourceBackend.EventSource.ConstructionException != null) throw eventSourceBackend.EventSource.ConstructionException; LoggingServices.DefaultBackend = eventSourceBackend;
As a result of this procedure, PostSharp Logging will emit records to the ETW event source named PostSharp-Patterns-Diagnostics. You now need to attach a listener to this event source.
Listening to ETW events
ETW is a complex system, and explaining it is beyond the scope of this documentation. This section shows only how to collect and view ETW events on your development machine.
The procedure uses a tool named PerfView, developed by Microsoft.
To collect and view PostSharp Logging logs using PerfView:
Download PerfView from the official Microsoft website.
Before starting your application, execute the following command line:
perfview.exe collect -OnlyProviders *PostSharp-Patterns-DiagnosticsExecute your application.
In PerfView, click Stop collecting, then in the PerfView tree view click on PerfViewData.etl.zip and finally Events.
To get a coherent view of all trace events, select all event types of the
PostSharp-Patterns-Diagnosticscategory at the same time.
See Also
Reference
Other Resources