【问题标题】:How to serialize user defined objects to WriteEvent?如何将用户定义的对象序列化为 WriteEvent?
【发布时间】:2015-12-02 21:28:24
【问题描述】:

根据 MSDN 文档,Write-event 仅支持 int 和 string 参数类型。我想将用户创建的 ct 传递给 Write-event,如何获得此功能?实现此功能的正确序列化程序是什么?

【问题讨论】:

  • Xml、Json、自定义等。正确的序列化程序取决于您在写出数据时打算如何处理数据,以及您打算如何读取它(人工或代码)。

标签: c# etw etw-eventsource


【解决方案1】:

有几种选择:

  • 按照 magicandre1981 的建议使用 the new .NET 4.6 feature。这是更可取的选择,除非您必须使用 .NET 4.5、4.0 甚至 3.5。
  • 以 JSON 或 Bond 等方式手动序列化它们
  • 手动创建EventData 结构并传递给WriteEventCore(必须在unsafe 内部)-TPL 当前使用此方法。
  • 或者你可以this 例如。有a blog post关于那个。

【讨论】:

【解决方案2】:

在 Windows 10(也向后移植到 Win7/8.1)中,自 .Net 4.6 起支持 Rich Payload Data

// 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 });

阅读博客和文档了解更多详情。

【讨论】:

  • 富负载数据无法传递给 WriteEvent 方法。需要传递给“public void Write(string eventName, T data);”事件源的方法。否则我错过了一些东西。
猜你喜欢
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 2019-01-29
相关资源
最近更新 更多