Field Medic and EventSource

Back to index Matthieu Maitre

A previous blog covered how to use Field Medic to capture event logs on Windows Phone. This is only half of the story though: something needs to generate those events in the first place. This is where EventSource comes into play. It enables quickly adding performance events (markers for operation beginning and end, etc.) and unstructured traces (aka printf) to .NET apps:

Log.Write("About to process file {0}", filename);
await ProcessImageAsync(filename);


EventSource does so with fairly little supporting code:

using System.Diagnostics.Tracing;

[EventSource(Name = "Company-Product-Component")]
sealed class Log : EventSource
    public static Log Events = new Log();

    // Unstructured traces

    public static void Write(string message) 

    public static void Write(string format, params object[] args) 
        if (Events.IsEnabled())
            Events.Message(String.Format(format, args));

    [Event(1, Level = EventLevel.Verbose)]
    private void Message(string message) { Events.WriteEvent(1, message); }

    // Performance markers

    public class Tasks
        public const EventTask ProcessImage = (EventTask)1;

    [Event(2, Task = Tasks.ProcessImage, Opcode = EventOpcode.Start, Level = EventLevel.Informational)]
    public void ProcessImageStart() { Events.WriteEvent(2); }

    [Event(3, Task = Tasks.ProcessImage, Opcode = EventOpcode.Stop, Level = EventLevel.Informational)]
    public void ProcessImageStop() { Events.WriteEvent(3); }

Field Medic supports capturing events generated by EventSource using typical WPRP config files, with one caveat: the event provider name must be prefixed by ‘*’.

<EventProvider Id="EventProvider_Company_Product_Component" Name="*Company-Product-Component" Level="5"/>

See Component.wprp for a full WPRP config file.