I would like to know what are yours experiences regarding using EventSource for components that are common and that can be utilized few times within the same process.
One simple example. My shared component is TestQueue and I would like to utilize it few times with my process and again in PerfView to see what event belongs to which queue.
public class TestQueue<T>
{
private readonly IEtwQueue _etwQueue;
private readonly Queue<T> _instance = new Queue<T>();
public TestQueue(IEtwQueue etwQueue)
{
_etwQueue = etwQueue;
}
public void Enqueue(T item)
{
_instance.Enqueue(item);
_etwQueue.CommandEnqueued(_instance.Count);
}
public T Dequeue()
{
_etwQueue.CommandDequed(_instance.Count);
return _instance.Dequeue();
}
}
public interface IEtwQueue
{
void CommandEnqueued(int items);
void CommandDequed(int items);
}
[EventSource(Name = "Test-ETW-Queue")]
public class EtwQueue : EventSource, IEtwQueue
{
public static EtwQueue Log = new EtwQueue();
private EtwQueue() { }
[Event(1)]
public void CommandEnqueued(int items) { if (IsEnabled()) WriteEvent(1, items); }
[Event(2)]
public void CommandDequed(int items) { if (IsEnabled()) WriteEvent(2, items); }
}
And I would like to use it like this:
TestQueue<string> testStringQueue = new TestQueue<string>(EtwQueue.Log);
TestQueue<int> testIntQueue = new TestQueue<int>(EtwQueue.Log);
testIntQueue.Enqueue(15);
testStringQueue.Enqueue("X");
Here is what I have in PerfView:
There is no difference between these two events. I would like to know how could I identify them so that some name (string) or ID figures out as part of event name? I know that I could use Tasks for logical grouping of events, but it is not what I would expect here, especially since they must be predefined in the event source. Activity ID is also the same in the use case.
Cheers!
One better way then implementing two EventSources is passing event source name through constructor of custom implementation. Maybe this is the way to do it :)
So there is no EventSource attribute on custom event source class and constructor has event source name parameter:
public class EtwQueueEventSource : EventSource, IEtwQueue
{
public EtwQueueEventSource(string sourceName) : base(sourceName) { }
[Event(1)]
public void CommandEnqueued(int items) { if (IsEnabled()) WriteEvent(1, items); }
[Event(2)]
public void CommandDequed(int items) { if (IsEnabled()) WriteEvent(2, items); }
}
So previous usage example becomes something like:
TestQueue<string> testStringQueue = new TestQueue<string>(new EtwQueueEventSource("Test-ETW-Queue-String"));
TestQueue<int> testIntQueue = new TestQueue<int>(new EtwQueueEventSource("Test-ETW-Queue-Integer"));
testIntQueue.Enqueue(15);
testStringQueue.Enqueue("X");