2015-12-02 61 views
1

從MSDN文檔中,寫事件只支持int和字符串參數類型。我想將用戶創建的ct傳遞給Write事件,如何獲得此功能?什麼是正確的序列化器來實現這個功能?如何將用戶定義的對象序列化爲WriteEvent?

+1

Xml,Json,custom等等正確的序列化依賴於你在數據寫出時打算做什麼,以及你打算如何讀取它(人或代碼)。 –

回答

0

在Windows 10(也回遷到Win7的/ 8.1)有對Rich Payload Data支持,因爲淨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 }); 

閱讀博客和文件的更多細節。

2

有幾種選擇:

  • 使用the new .NET 4.6 feature爲magicandre1981建議。除非你必須使用.NET 4.5,4.0甚至3.5,否則這是更好的選擇。
  • 手動它們序列化在JSON或債券或等
  • 手動創建EventData struct和傳遞給WriteEventCore(必須是內部unsafe) - TPL目前使用這種方法。
  • 或者你可以例如this。關於這個,有a blog post
+1

FWIW,Jon Wagner在創建/生成EventSource時有一個[有趣的事情](https://github.com/jonwagner/EventSourceProxy) – Benjol

相關問題