7

I'd like to be able to declare an EventSource which has a minimum of several methods which by default provide regular logging facilities.

e.g.

  • Info()
  • Warn()
  • Error()

In addition I'd like to be able to within each service, define a specific event source that inherits from the base class providing the above. At the moment the EventRegister.exe app which creates the manifest complains that the event source must be sealed.

Am I doing this wrong? If so how can I achieve the above? See example code:

    public class ETWBase : EventSource
    {
        [Event(1, Channel = EventChannel.Admin, Message = "Info Message: {0}")]
        public void Info(string message) { this.WriteEvent(1); }

        [Event(2, Channel = EventChannel.Debug, Message = "Debug Message: {0}")]
        public void Trace(string message) { this.WriteEvent(2); }

    }

    [EventSource(Name = "ABC-MyEtwServiceEventSource")]
    public sealed class MyEtwServiceEventSource : ETWBase
    {
        public static MyEtwServiceEventSource Log = new MyEtwServiceEventSource();

        [Event(3, Channel = EventChannel.Debug, Message = "My specific Message: {0}")]
        public void Trace(string message) { this.WriteEvent(3); }       
    }

I'm using the latest and greatest Microsoft.Diagnostics.Tracing (pre) which I understand has support for Channels unlike the SLAB from Enterprise Library.

magicandre1981
  • 27,895
  • 5
  • 86
  • 127
jaffa
  • 26,770
  • 50
  • 178
  • 289
  • 1
    Microsoft.Diagnostics.Tracing (pre) expects the class to be sealed. The class inheriting EventSource needs to be sealed, you will not be able to generate manifest, if you dont mark it as sealed. I doubt whether this will work. – KRP Sep 25 '13 at 11:22
  • How would I share common methods across multiple EventSources across a wide system? – jaffa Sep 25 '13 at 12:55
  • I am not really sure on that front, i think we can't do that. – KRP Sep 25 '13 at 13:46
  • 1
    the stable version should now allow what you want according to the blog: http://blogs.msdn.com/b/dotnet/archive/2014/01/30/microsoft-diagnostics-tracing-eventsource-rtms.aspx?Redirected=true "EventSource types may now implement interfaces" and "The concept of a utility event source type (defined as an abstract class deriving from EventSource) is introduced to support sharing code across multiple event source types in a project (e.g. for optimized WriteEvent() overloads)." – magicandre1981 Apr 18 '14 at 07:25
  • Ok thanks for this. I became aware of the development but by then I had already implemented my ETW library as a NuGet package containing the pre-written source as a partial class which could then be added to. – jaffa Apr 22 '14 at 10:19
  • Two years has passed — the situation still the same: we are not able to declare some events in a base class as events and not only as “helper” methods. – cassandrad Jun 08 '16 at 13:00

1 Answers1

3

ETWBase should be abstract and should not have methods decorated by EventAttribute.

You could find more information in documentation file _EventSourceUsersGuide.docx that is being added to your project if you are referencing Event Source or Event Source Samples nuget packages.

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198