Logging with ETW
This article shows how to use PostSharp Logging and Event Tracing for Windows (ETW) together.
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.Tracing package 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;
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 very complex system and explaining it is beyong the scope of this documentation. Let's just show how you can collect and view ETW events on your development machine.
We will use 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-Diagnostics
Execute 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, it is necessary to simultaneously select all event types of the