Question

I want to get the Event Provider Guid based on Event Provider Name(Ex: Sample-Test)

Sample Code

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

Here is my provider

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

}

My logger class for logging events

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

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

    ...
}

Please suggest the code necessary to get GUID based on EventSourceName. I have already registered with Eventvwr.

Was it helpful?

OTHER TIPS

I used PerfView to get the GUID. Start a capturing, enable the provider you want to know the GUID, start the logging, go to "log" entry and here you see the GUID.

When you registered the Provider system-wide you can use use xperf -providers to see the GUID.

The following appears to be the algorithm in a less obfuscated form:

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

This is my quick and dirty way to do this. It works fine for the utility program I use it in, but I'd cache the mappings in a long running production application to avoid unintended consequences of create event sources.

    private static Guid GenerateGuidFromName(string name)
    {
        var eventSource = new EventSource(name);
        return eventSource.Guid;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top