the rock
the rock

Reputation: 11

How to serialize user defined objects to WriteEvent?

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?

Upvotes: 1

Views: 837

Answers (2)

Sergey Baranchenkov
Sergey Baranchenkov

Reputation: 624

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 to WriteEventCore (has to be inside unsafe) - TPL currently uses this method.
  • Or you could this for example. There is a blog post about that.

Upvotes: 2

magicandre1981
magicandre1981

Reputation: 28826

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.

Upvotes: 1

Related Questions