{"id":4801,"date":"2015-09-30T13:00:33","date_gmt":"2015-09-30T13:00:33","guid":{"rendered":"http:\/\/writeasync.net\/?p=4801"},"modified":"2015-09-30T04:32:06","modified_gmt":"2015-09-30T04:32:06","slug":"eventsource-and-rich-payload-data","status":"publish","type":"post","link":"https:\/\/writeasync.net\/?p=4801","title":{"rendered":"EventSource and rich payload data"},"content":{"rendered":"<p>Long ago, <a href=\"http:\/\/writeasync.net\/?p=1321\">I wrote about EventSource<\/a>, the simple and modern ETW-based tracing solution for .NET 4.5+. Much to the diagnosticians&#8217; delight, <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.diagnostics.tracing.eventsource(v=vs.110).aspx\">EventSource<\/a> continues to evolve in <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms171868(v=vs.110).aspx#v46\">.NET 4.6<\/a>. Check out Vance Morrison&#8217;s post describing <a href=\"http:\/\/blogs.msdn.com\/b\/vancem\/archive\/2015\/09\/20\/rich-payload-data-in-eventsource-v4-6.aspx\">the useful new &#8220;rich payload data&#8221; feature<\/a>.<\/p>\n<p>Here is a minimal example showing a new manifest-free way to log such semi-structured data with <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dn823294(v=vs.110).aspx\"><code>EventSource.Write&lt;T&gt;<\/code><\/a>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nnamespace RichPayloadDataSample\r\n{\r\n    using System;\r\n    using System.Diagnostics.Tracing;\r\n    using System.Threading;\r\n    using System.Threading.Tasks;\r\n\r\n    internal sealed class Program\r\n    {\r\n        private static void Main(string&#x5B;] args)\r\n        {\r\n            using (CancellationTokenSource cts = new CancellationTokenSource())\r\n            {\r\n                Task loop = EventLoop(cts.Token);\r\n                Console.WriteLine(&quot;Press ENTER to quit.&quot;);\r\n                Console.ReadLine();\r\n\r\n                cts.Cancel();\r\n                loop.Wait();\r\n            }\r\n        }\r\n\r\n        private static async Task EventLoop(CancellationToken token)\r\n        {\r\n            await Task.Yield();\r\n            MyEventSource myEvents = MyEventSource.Instance;\r\n            int n = 0;\r\n            try\r\n            {\r\n                while (!token.IsCancellationRequested)\r\n                {\r\n                    ++n;\r\n                    Console.WriteLine(&quot;&#x5B;{0}] Iteration {1}.&quot;, Thread.CurrentThread.ManagedThreadId, n);\r\n\r\n                    myEvents.Write(&quot;SomethingGood&quot;, new GoodStuff { Name = &quot;X&quot; + n, Number = n });\r\n                    await Task.Delay(500, token);\r\n\r\n                    myEvents.Write(&quot;SomethingBad&quot;, new BadStuff { Name = &quot;Z&quot; + n, Number = -n });\r\n                    await Task.Delay(500, token);\r\n                }\r\n            }\r\n            catch (OperationCanceledException)\r\n            {\r\n            }\r\n        }\r\n\r\n        &#x5B;EventData]\r\n        private sealed class GoodStuff\r\n        {\r\n            public string Name { get; set; }\r\n\r\n            public int Number { get; set; }\r\n        }\r\n\r\n        &#x5B;EventData]\r\n        private sealed class BadStuff\r\n        {\r\n            public string Name { get; set; }\r\n\r\n            public int Number { get; set; }\r\n        }\r\n\r\n        &#x5B;EventSource(Guid = &quot;554283bf-0ce9-4216-a925-f8629295ba1d&quot;)]\r\n        private sealed class MyEventSource : EventSource\r\n        {\r\n            public static readonly MyEventSource Instance = new MyEventSource();\r\n\r\n            private MyEventSource()\r\n            {\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Let&#8217;s see what this data looks like in plain old ETL format. Using <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/bb490956.aspx\"><code>logman<\/code><\/a>, we can log the events to a file (remember, elevated command prompt):<\/p>\n<pre>\r\nlogman create trace RichPayload -p {554283bf-0ce9-4216-a925-f8629295ba1d} 0xFFFFFFFF 5 -ow -o RichPayload.etl\r\nlogman start RichPayload\r\n<em>[ ... at this point, we run the app for a few seconds and quit ... ]<\/em>\r\nlogman stop RichPayload\r\n<\/pre>\n<p>With <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/bb490959.aspx\">tracerpt<\/a>, we can then decode the traces into EVTX format for easy viewing:<\/p>\n<pre>\r\ntracerpt -l RichPayload_000001.etl  -of evtx -o RichPayload.evtx\r\n<\/pre>\n<p>Now, open the EVTX file in <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/cc938674.aspx\">Event Viewer<\/a> and look at the event payload in the Details tab, selecting XML View. You will see output like the following:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Event xmlns=&quot;http:\/\/schemas.microsoft.com\/win\/2004\/08\/events\/event&quot;&gt;\r\n  &lt;System&gt;\r\n    &lt;Provider Name=&quot;MyEventSource&quot; Guid=&quot;{554283bf-0ce9-4216-a925-f8629295ba1d}&quot; \/&gt;\r\n    &lt;EventID&gt;2&lt;\/EventID&gt;\r\n    &lt;Version&gt;0&lt;\/Version&gt;\r\n    &lt;Level&gt;5&lt;\/Level&gt;\r\n    &lt;Task&gt;0&lt;\/Task&gt;\r\n    &lt;Opcode&gt;0&lt;\/Opcode&gt;\r\n    &lt;Keywords&gt;0x0&lt;\/Keywords&gt;\r\n    &lt;TimeCreated SystemTime=&quot;2015-09-30T03:41:27.305685200Z&quot; \/&gt;\r\n    &lt;Correlation ActivityID=&quot;{00000000-0000-0000-0000-000000000000}&quot; \/&gt;\r\n    &lt;Execution ProcessID=&quot;7448&quot; ThreadID=&quot;14300&quot; ProcessorID=&quot;11&quot; KernelTime=&quot;0&quot; UserTime=&quot;0&quot; \/&gt;\r\n    &lt;Channel \/&gt;\r\n    &lt;Computer \/&gt;\r\n  &lt;\/System&gt;\r\n  &lt;EventData&gt;\r\n    &lt;Data Name=&quot;Name&quot;&gt;X1&lt;\/Data&gt;\r\n    &lt;Data Name=&quot;Number&quot;&gt;1&lt;\/Data&gt;\r\n  &lt;\/EventData&gt;\r\n  &lt;RenderingInfo Culture=&quot;en-US&quot;&gt;\r\n    &lt;Task&gt;SomethingGood&lt;\/Task&gt;\r\n  &lt;\/RenderingInfo&gt;\r\n&lt;\/Event&gt;\r\n<\/pre>\n<p>Note the <code>EventData<\/code> payload which contains the key\/value pairs that were logged.<\/p>\n<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/hh448170.aspx\">Windows Performance Analyzer<\/a> is smart enough to decode these events as well. It shows data of this type as generic fields with names derived from the event payload:<br \/>\n<a href=\"http:\/\/writeasync.net\/wp-content\/uploads\/2015\/09\/wpa-richData.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/writeasync.net\/wp-content\/uploads\/2015\/09\/wpa-richData-300x215.png\" alt=\"WPA rich data sample\" width=\"300\" height=\"215\" class=\"alignnone size-medium wp-image-4841\" srcset=\"https:\/\/writeasync.net\/wp-content\/uploads\/2015\/09\/wpa-richData-300x215.png 300w, https:\/\/writeasync.net\/wp-content\/uploads\/2015\/09\/wpa-richData-474x342.png 474w, https:\/\/writeasync.net\/wp-content\/uploads\/2015\/09\/wpa-richData.png 966w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Obviously there are pros and cons to manifest-based events and rich data payloads. As Vance points out, performance of rich data is going to be substantially worse when compared to manifest-based events. So don&#8217;t go converting all your events to rich data payloads just yet&#8230; However, it&#8217;s a useful capability to have in your tracing arsenal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Long ago, I wrote about EventSource, the simple and modern ETW-based tracing solution for .NET 4.5+. Much to the diagnosticians&#8217; delight, EventSource continues to evolve in .NET 4.6. Check out Vance Morrison&#8217;s post describing the useful new &#8220;rich payload data&#8221;&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-4801","post","type-post","status-publish","format-standard","hentry","category-diagnostics"],"_links":{"self":[{"href":"https:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/4801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4801"}],"version-history":[{"count":0,"href":"https:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/4801\/revisions"}],"wp:attachment":[{"href":"https:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}