Skip to content

Instantly share code, notes, and snippets.

@meziantou
Last active August 29, 2015 14:03
Show Gist options
  • Save meziantou/6b577076525947d90829 to your computer and use it in GitHub Desktop.
Save meziantou/6b577076525947d90829 to your computer and use it in GitHub Desktop.
ETW
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<remove name="Default" />
<add name="EtwListener"
type="System.Diagnostics.Eventing.EventProviderTraceListener, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
initializeData="{A6871CD5-0885-4E6E-AE3E-E62E27FF02BE}" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
using System;
using System.Diagnostics;
using System.Diagnostics.Eventing;
namespace Etw
{
class Program
{
static void Main(string[] args)
{
using (EtwLogger logger = new EtwLogger())
{
for (int i = 0; i < 10000; i++)
{
logger.Log(LogType.Information, "Test " + i);
}
}
for (int i = 0; i < 10000; i++)
{
Trace.WriteLine("Test " + i);
}
// TraceSource
Guid providerGuid = new Guid("A6871CD5-0885-4E6E-AE3E-E62E27FF02BF");
EventProviderTraceListener listener = new EventProviderTraceListener(providerGuid.ToString(), "Test ETW Listener", "::");
TraceSource source = new TraceSource("MyProvider", SourceLevels.All);
source.Listeners.Add(listener);
source.TraceData(TraceEventType.Error, 2, new object[] { "abc", "def", true, 123 });
source.TraceEvent(TraceEventType.Warning, 12, "Provider guid: {0}", new object[] { providerGuid });
source.TraceInformation("string {0}, bool {1}, int {2}, ushort {3}", new object[] { "abc", false, 123, 5 });
}
}
public enum LogType
{
None = 0,
Fatal = 1,
Error = 2,
Warning = 3,
Information = 4,
Verbose = 5,
}
public class EtwLogger : IDisposable
{
public static Guid ProviderGuid = new Guid("A6871CD5-0885-4E6E-AE3E-E62E27FF02BD");
private EventProvider _provider = new EventProvider(ProviderGuid);
public virtual void Log(LogType type, string message)
{
if (_provider == null)
return;
_provider.WriteMessageEvent(message, (byte)type, 0);
}
public void Dispose()
{
if (_provider != null)
{
_provider.Dispose();
_provider = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment