繁体   English   中英

如何将用户定义的对象序列化为WriteEvent?

[英]How to serialize user defined objects to WriteEvent?

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

有几种选择:

  • 使用magicandre1981建议的新.NET 4.6功能 除非您必须使用.NET 4.5、4.0甚至3.5,否则这是更可取的选择。
  • 手动序列化JSON或Bond等
  • 手动创建EventData结构,并传递给WriteEventCore (必须是内部unsafe ) - TPL目前使用这种方法。
  • 或者您可以举这个例子。 一篇关于此的博客文章

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

阅读博客和文档以获取更多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM