2013-09-27 92 views
1

我想基於事件提供程序名稱來獲取事件提供的Guid ETW提供的Guid(例如:樣品測試)基於名稱.NET 4.0

示例代碼

[EventSource(Name = "Sample-Test")] 
public sealed class EventSourceLogger : EventSource 

這裏是我的供應商

internal class EventProviderVersionOne : EventProvider 
{ 
    internal EventProviderVersionOne(Guid id) 
     : base(id) 
    { } 

    [StructLayout(LayoutKind.Explicit, Size = 16)] 
    private struct EventData 
    { 
     [FieldOffset(0)] 
     internal UInt64 DataPointer; 
     [FieldOffset(8)] 
     internal uint Size; 
     [FieldOffset(12)] 
     internal int Reserved; 
    } 

} 

我的記錄器類記錄事件

public class EventLogger 
{ 
    public static EventLogger Log = new EventLogger(); 

    internal static EventProviderVersionOne MProvider = new EventProviderVersionOne(new Guid(ConfigurationSettings.AppSettings["EtwEventProviderGuid"])); 

    ... 
} 

請根據EventSourceName提示獲取GUID所需的代碼。我已經註冊了Eventvwr。

回答

0

我使用PerfView來獲取GUID。開始捕獲,啓用您想知道GUID的提供程序,啓動日誌記錄,轉到「日誌」條目,在這裏您可以看到GUID。

當您在系統範圍內註冊Provider時,可以使用使用xperf -providers來查看GUID。

0

下面似乎是一個不太混淆形式的算法:

public static byte[] Concat(byte[] a, byte[] b) 
    { 
     byte[] retval = new byte[a.Length + b.Length]; 
     a.CopyTo(retval, 0); 
     b.CopyTo(retval, a.Length); 
     return retval; 
    } 

    public static byte[] Slice(byte[] a, int startIndex, int length) 
    { 
     byte[] retval = new byte[length]; 
     Array.Copy(a, startIndex, retval, 0, length); 
     return retval; 
    } 

    private static Guid GenerateGuidFromName(string name) 
    { 
     byte[] namespaceGuid = Guid.Parse("b22d2c48-90c3-c847-87f8-1a15bfc130fb").ToByteArray(); 
     byte[] nameBytes = Encoding.BigEndianUnicode.GetBytes(name); 
     byte[] sha1Hash = SHA1.Create().ComputeHash(Concat(namespaceGuid, nameBytes)); 
     byte[] guidBytes = Slice(sha1Hash, 0, 16); 
     // Overwrite the top 4 bits of the 8th byte with 0101 
     { 
      guidBytes[7] &= 0x0F; 
      guidBytes[7] |= 0x50; 
     } 
     return new Guid(guidBytes); 
    } 
+1

但是不要忘了做ToUpperInvariant()首先.... – paullem