user884248
user884248

Reputation: 2244

.Net: subclassing subclasses of EventSource

I have a C# solution which spans many, many projects. I created a tracing class that inherits from Systme.Diagnostics.Tracing.EventSource, called MyCustomEventSource, which handles most of my tracing events. I want to create subclasses of this class for each project, so that I can implement events relevant to specific projects.

I am also writing an ETW consumer where I can easily listen to events from the different components of my system.

But my consumer doesn't recognize events correctly: when I listen to MyCustomEventSource, the EventName field of each event is set to the name of the method, and FormattedMessage is set to the Message attribute. Example:

[Event(1,
         Message = "The configuration parameter \"{0}\" was loaded with a value of {1}.",
         Level = EventLevel.Informational,
         Task = Tasks.Configuration,
         Keywords = Keywords.Diagnostics)]
    public void ConfigParameterLoaded(string name, string value)
    {
        WriteEvent(1, name, value);
    }

If I listen to MyCustomEventSource, and an app calls ConfigParameterLoaded, then I get an event whose name is ConfigParameterLoaded, and its FormattedMessage is "The configuration parameter "x" was loaded with a value of y."

This is fine.

But, if I create a similar method on the subclass of MyCustomEventSource and trigger events from this subclass, my EventName will always be "EventSourceMessage", and the FormattedMessage will always be null.

I figure this is probably because System.Diagnostics.Tracing.EventAttribute doesn't have AttributeUsage(Inherited=true).

My question is: is there some way to get around this, and get the correct information when triggering events from a subclass?

Upvotes: 3

Views: 380

Answers (1)

magicandre1981
magicandre1981

Reputation: 28776

This does't work because it is not allowed. You are only allowed to implement interface and derive from abstract class that doesn't include any events, tasks, keywords. Read the _EventSourceUsersGuide.docx:

Starting with the RTM release of the EventSource NuGet package it is now possible to specify event source types that are implementing an interface. The initial design decision (in the beta release of the NuGet package) to explicitly disallow event source class hierarchies. In the new approach one can define utility event source types: abstract event source classes that derive from EventSource and hold all code common to a set of event sources. These abstract classes cannot define any ETW-specific elements: keywords, tasks, opcodes, channels, events. They can only provide methods to be used by derived classes.

Upvotes: 4

Related Questions