Skip to content

Instantly share code, notes, and snippets.

@naveensrinivasan
Created May 29, 2015 03:16
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 naveensrinivasan/83ded09f7d754ad0b3a8 to your computer and use it in GitHub Desktop.
Save naveensrinivasan/83ded09f7d754ad0b3a8 to your computer and use it in GitHub Desktop.
Log dynamic Custom objects in ETW using EventSource
using System;
using System.Collections.Generic;
using Microsoft.Diagnostics.Tracing;
namespace ETWSamples
{
/// <summary>
/// How to generate dynamic etw events without inheriting and eventsource class.
/// This is not advisable.
/// </summary>
public class DynamicEventSource
{
/// <summary>
/// The new event source provides an option to log anonymous method.
/// </summary>
static EventSource trace = new EventSource("Dynamic");
public static void LogAnonymousObject() {
for (int i = 0; i < 10; i++)
{
trace.Write("Test", new { Name = "Dynamic Event",
Number = i, Date = DateTime.Now });
}
}
/// <summary>
/// This method logs the custom class with the EventData
/// attribute. It will serialize things like date,int,string,IEnumerable.
/// It will not be able to serialize custom objects.
/// </summary>
public static void LogCustomClass()
{
trace.Write("Custom", new CustomClass()
{
Name = "Custom",
Date = DateTime.Now,
Number = 100,
Countries = new[] { "USA", "India" },
SSN="99999999"
});
}
}
[EventData]
public class CustomClass
{
public string Name { get; set; }
public int Number { get; set; }
public DateTime Date { get; set; }
public IEnumerable<string> Countries { get; set; }
[EventIgnore]
public string SSN { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment