From MSDN documentation, Write-event supports only int and string param types. I want to pass a user-created ct to Write-event, how to get this functionality? What would be the right serializer to achieve this functionality?
Asked
Active
Viewed 781 times
1
-
1Xml, Json, custom, etc. The right serializer depends on what you intend to do with the data when its written out, and how you intend to read it (human or code). – Ron Beyer Dec 02 '15 at 21:33
2 Answers
2
There are several options:
- Use the new .NET 4.6 feature as magicandre1981 suggested. And that is the preferable option unless you have to use .NET 4.5, 4.0 or even 3.5.
- Manually serialize them in JSON or Bond or etc
- Manually create the
EventData
struct and pass toWriteEventCore
(has to be insideunsafe
) - TPL currently uses this method. - Or you could this for example. There is a blog post about that.

snipsnipsnip
- 2,268
- 2
- 33
- 34

Sergey Baranchenkov
- 614
- 6
- 11
-
2FWIW, Jon Wagner has an [interesting take](https://github.com/jonwagner/EventSourceProxy) on creating/generating EventSource – Benjol Jan 14 '16 at 14:02
1
In Windows 10 (also backported to Win7/8.1) there is support for Rich Payload Data since .Net 4.6:
// define a ‘plain old data’ class
[EventData]
class SimpleData {
public string Name { get; set; }
public int Address { get; set; }
}
[EventSource(Name = "Samples-EventSourceDemos-RuntimeDemo")]
public sealed class RuntimeDemoEventSource : EventSource
{
// define the singleton instance of the event source
public static RuntimeDemoEventSource Log = new RuntimeDemoEventSource();
// Rich payloads only work when the self-describing format
private RuntimeDemoEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat) { }
// define a new event.
public void LogSimpleData(string message, SimpleData data) { WriteEvent(1, message, data); }
}
RuntimeDemoEventSource.Log.LogSimpleData(
"testMessage",
new SimpleData() { Name = "aName", Address = 234 });
Read the blog and document for more details.

magicandre1981
- 27,895
- 5
- 86
- 127
-
Rich Payload Data cannot be passed to WriteEvent method. It needs to be passed to "public void Write
(string eventName, T data);" method of EventSource. Else I am missing something. – Dharmesh Tailor May 03 '18 at 23:07