【问题标题】:ETW Provider Guid based on Name .Net 4.0基于名称 .Net 4.0 的 ETW 提供者指南
【发布时间】:2013-09-27 08:46:05
【问题描述】:

我想根据事件提供者名称获取事件提供者 Guid(例如:Sample-Test)

示例代码

[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。

【问题讨论】:

    标签: .net metadata manifest etw etw-eventsource


    【解决方案1】:
    【解决方案2】:

    我使用 PerfView 来获取 GUID。开始捕获,启用您想知道 GUID 的提供程序,启动日志记录,转到“日志”条目,在这里您可以看到 GUID。

    当您在系统范围内注册提供程序时,您可以使用 xperf -providers 查看 GUID。

    【讨论】:

      【解决方案3】:

      以下似乎是模糊程度较低的算法:

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

      【讨论】:

        【解决方案4】:

        这是我快速而肮脏的方法。它适用于我使用它的实用程序,但我会将映射缓存在长时间运行的生产应用程序中以避免创建事件源的意外后果。

            private static Guid GenerateGuidFromName(string name)
            {
                var eventSource = new EventSource(name);
                return eventSource.Guid;
            }
        

        【讨论】:

          猜你喜欢
          • 2012-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-01
          • 1970-01-01
          • 2018-10-20
          • 1970-01-01
          • 2019-02-25
          相关资源
          最近更新 更多