Skip to content

Instantly share code, notes, and snippets.

@noahfalk
Created May 8, 2021 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahfalk/ca503276cea4b97c6630ebe672318ece to your computer and use it in GitHub Desktop.
Save noahfalk/ca503276cea4b97c6630ebe672318ece to your computer and use it in GitHub Desktop.
Create and collect ETW events from a custom EventSource
// Steps to run this sample:
// 1. Create a console app and replace Program.cs with this source
// 2. Build the app (for example with dotnet build). My exe is produced in
// "E:\temp\EventSourceSample\bin\Debug\net5.0\EventSourceSample.exe"
// 3. Download PerfView if you don't already have it: https://github.com/microsoft/perfview/blob/main/documentation/Downloading.md
// 4. In an admin command prompt, run this PerfView command substituting the path to your executable:
// PerfView.exe -OnlyProviders:MyEventSource run E:\temp\EventSourceSample\bin\Debug\net5.0\EventSourceSample.exe
// 5. After PerfView finishes collecting, open the Events view and confirm that 1 event of type MyEventSource/HelloWorld was collected in
// addition to the other events
using System;
using System.Diagnostics.Tracing;
namespace EventSourceSample
{
class Program
{
static void Main(string[] args)
{
MyEventSource source = new MyEventSource();
source.HelloWorld();
}
}
[EventSource(Name="MyEventSource")]
class MyEventSource : EventSource
{
[Event(1)]
public void HelloWorld()
{
WriteEvent(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment