EventSource Class

EventSource Class

.NET Framework (current version)
 

Provides the ability to create events for event tracing for Windows (ETW).

Namespace:   System.Diagnostics.Tracing
Assembly:  mscorlib (in mscorlib.dll)

System.Object
  System.Diagnostics.Tracing.EventSource

public class EventSource : IDisposable

NameDescription
System_CAPS_protmethodEventSource()

Creates a new instance of the EventSource class.

System_CAPS_protmethodEventSource(Boolean)

Creates a new instance of the EventSource class and specifies whether to throw an exception when an error occurs in the underlying Windows code.

System_CAPS_protmethodEventSource(EventSourceSettings)

Creates a new instance of the EventSource class with the specified configuration settings.

System_CAPS_protmethodEventSource(EventSourceSettings, String[])

Initializes a new instance of the EventSource to be used with non-contract events that contains the specified settings and traits.

System_CAPS_pubmethodEventSource(String)

Creates a new instance of the EventSource class with the specified name.

System_CAPS_pubmethodEventSource(String, EventSourceSettings)

Creates a new instance of the EventSource class with the specified name and settings.

System_CAPS_pubmethodEventSource(String, EventSourceSettings, String[])

Creates a new instance of the EventSource class with the specified configuration settings.

NameDescription
System_CAPS_pubpropertyConstructionException

[Supported in the .NET Framework 4.5.1 and later versions]

Gets any exception that was thrown during the construction of the event source.

System_CAPS_pubpropertySystem_CAPS_staticCurrentThreadActivityId

[Supported in the .NET Framework 4.5.1 and later versions]

Gets the activity ID of the current thread.

System_CAPS_pubpropertyGuid

The unique identifier for the event source.

System_CAPS_pubpropertyName

The friendly name of the class that is derived from the event source.

System_CAPS_pubpropertySettings

Gets the settings applied to this event source.

NameDescription
System_CAPS_pubmethodDispose()

Releases all resources used by the current instance of the EventSource class.

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the EventSource class and optionally releases the managed resources.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows the EventSource object to attempt to free resources and perform other cleanup operations before the object is reclaimed by garbage collection.(Overrides Object.Finalize().)

System_CAPS_pubmethodSystem_CAPS_staticGenerateManifest(Type, String)

Returns a string of the XML manifest that is associated with the current event source.

System_CAPS_pubmethodSystem_CAPS_staticGenerateManifest(Type, String, EventManifestOptions)

Returns a string of the XML manifest that is associated with the current event source.

System_CAPS_pubmethodSystem_CAPS_staticGetGuid(Type)

Gets the unique identifier for this implementation of the event source.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticGetName(Type)

Gets the friendly name of the event source.

System_CAPS_pubmethodSystem_CAPS_staticGetSources()

Gets a snapshot of all the event sources for the application domain.

System_CAPS_pubmethodGetTrait(String)

Gets the trait value associated with the specified key.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIsEnabled()

Determines whether the current event source is enabled.

System_CAPS_pubmethodIsEnabled(EventLevel, EventKeywords)

Determines whether the current event source that has the specified level and keyword is enabled.

System_CAPS_pubmethodIsEnabled(EventLevel, EventKeywords, EventChannel)

Determines whether the current event source is enabled for events with the specified level, keywords and channel.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnEventCommand(EventCommandEventArgs)

Called when the current event source is updated by the controller.

System_CAPS_pubmethodSystem_CAPS_staticSendCommand(EventSource, EventCommand, IDictionary<String, String>)

Sends a command to a specified event source.

System_CAPS_pubmethodSystem_CAPS_staticSetCurrentThreadActivityId(Guid)

[Supported in the .NET Framework 4.5.1 and later versions]

Sets the activity ID on the current thread.

System_CAPS_pubmethodSystem_CAPS_staticSetCurrentThreadActivityId(Guid, Guid)

[Supported in the .NET Framework 4.5.1 and later versions]

Sets the activity ID on the current thread, and returns the previous activity ID.

System_CAPS_pubmethodToString()

Obtains a string representation of the current event source instance.(Overrides Object.ToString().)

System_CAPS_pubmethodWrite(String)

Writes an event without fields, but with the specified name and default options.

System_CAPS_pubmethodWrite(String, EventSourceOptions)

Writes an event without fields, but with the specified name and options.

System_CAPS_pubmethodWrite<T>(String, T)

Writes an event with the specified name and data.

System_CAPS_pubmethodWrite<T>(String, EventSourceOptions, T)

Writes an event with the specified name, event data and options.

System_CAPS_pubmethodWrite<T>(String, EventSourceOptions, T)

Writes an event with the specified name, options and event data.

System_CAPS_pubmethodWrite<T>(String, EventSourceOptions, Guid, Guid, T)

Writes an event with the specified name, options, related activity and event data.

System_CAPS_protmethodWriteEvent(Int32)

Writes an event by using the provided event identifier.

System_CAPS_protmethodWriteEvent(Int32, Byte[])

Writes an event by using the provided event identifier and byte array argument.

System_CAPS_protmethodWriteEvent(Int32, Int32)

Writes an event by using the provided event identifier and 32-bit integer argument.

System_CAPS_protmethodWriteEvent(Int32, Int32, Int32)

Writes an event by using the provided event identifier and 32-bit integer arguments.

System_CAPS_protmethodWriteEvent(Int32, Int32, Int32, Int32)

Writes an event by using the provided event identifier and 32-bit integer arguments.

System_CAPS_protmethodWriteEvent(Int32, Int32, String)

Writes an event by using the provided event identifier and 32-bit integer and string arguments.

System_CAPS_protmethodWriteEvent(Int32, Int64)

Writes an event by using the provided event identifier and 64-bit integer argument.

System_CAPS_protmethodWriteEvent(Int32, Int64, Byte[])

Writes the event data using the specified indentifier and 64-bit integer and byte array arguments.

System_CAPS_protmethodWriteEvent(Int32, Int64, Int64)

Writes an event by using the provided event identifier and 64-bit arguments.

System_CAPS_protmethodWriteEvent(Int32, Int64, Int64, Int64)

Writes an event by using the provided event identifier and 64-bit arguments.

System_CAPS_protmethodWriteEvent(Int32, Int64, String)

Writes an event by using the provided event identifier and 64-bit integer, and string arguments.

System_CAPS_protmethodWriteEvent(Int32, Object[])

Writes an event by using the provided event identifier and array of arguments.

System_CAPS_protmethodWriteEvent(Int32, String)

Writes an event by using the provided event identifier and string argument.

System_CAPS_protmethodWriteEvent(Int32, String, Int32)

Writes an event by using the provided event identifier and arguments.

System_CAPS_protmethodWriteEvent(Int32, String, Int32, Int32)

Writes an event by using the provided event identifier and arguments.

System_CAPS_protmethodWriteEvent(Int32, String, Int64)

Writes an event by using the provided event identifier and arguments.

System_CAPS_protmethodWriteEvent(Int32, String, String)

Writes an event by using the provided event identifier and string arguments.

System_CAPS_protmethodWriteEvent(Int32, String, String, String)

Writes an event by using the provided event identifier and string arguments.

System_CAPS_protmethodWriteEventCore(Int32, Int32, EventSource.EventData*)

Creates a new WriteEvent overload by using the provided event identifier and event data.

System_CAPS_protmethodWriteEventWithRelatedActivityId(Int32, Guid, Object[])

[Supported in the .NET Framework 4.5.1 and later versions]

Writes an event that indicates that the current activity is related to another activity.

System_CAPS_protmethodWriteEventWithRelatedActivityIdCore(Int32, Guid*, Int32, EventSource.EventData*)

[Supported in the .NET Framework 4.5.1 and later versions]

Writes an event that indicates that the current activity is related to another activity.

NameDescription
System_CAPS_pubeventEventCommandExecuted

Occurs when a command comes from an event listener.

This class is intended to be inherited by a user class that provides specific events to be used for ETW. The EventSource.WriteEvent methods are called to log the events.

System_CAPS_importantImportant

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

The basic functionality of EventSource is sufficient for most applications. If you want more control over the ETW manifest that is created, you can apply the EventAttribute attribute to the methods. For advanced event source applications, it is possible to intercept the commands being sent to the derived event source and change the filtering, or to cause actions (such as dumping a data structure) to be performed by the inheritor. An event source can be activated with Windows ETW controllers, such as the Logman tool, immediately. It is also possible to programmatically control and intercept the data dispatcher. The EventListener class provides additional functionality.

Starting with .NET Framework 4.6, EventSource provides channel support and some of the event source validation rules have been relaxed. This means:

  • EventSource types may now implement interfaces. This enables the use of event source types in advanced logging systems that use interfaces to define a common logging target.

  • The concept of a utility event source type has been introduced. This feature enables sharing code across multiple event source types in a project to enable scenarios such as optimized WriteEvent overloads.

For a version of the EventSource class that provides features such as channel support you are targeting .NET Framework 4.5.1 or earlier, see Microsoft EventSource Library 1.0.16.

The following example shows a simple implementation of the EventSource class.

using System.Diagnostics.Tracing;
using System.Collections.Generic;

namespace Demo1
{
    class MyCompanyEventSource : EventSource
    {
        public static MyCompanyEventSource Log = new MyCompanyEventSource();

        public void Startup() { WriteEvent(1); }
        public void OpenFileStart(string fileName) { WriteEvent(2, fileName); }
        public void OpenFileStop() { WriteEvent(3); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string name = MyCompanyEventSource.GetName(typeof(MyCompanyEventSource));
            IEnumerable<EventSource> eventSources = MyCompanyEventSource.GetSources();
            MyCompanyEventSource.Log.Startup();
            // ...
            MyCompanyEventSource.Log.OpenFileStart("SomeFile");
            // ...
            MyCompanyEventSource.Log.OpenFileStop();
        }
    }
}

The following example shows a more complex implementation of the EventSource class.

using System;
using System.Diagnostics.Tracing;

namespace Demo2
{
    enum MyColor { Red, Yellow, Blue };

    [EventSource(Name = "MyCompany")]
    class MyCompanyEventSource : EventSource
    {
        public class Keywords
        {
            public const EventKeywords Page = (EventKeywords)1;
            public const EventKeywords DataBase = (EventKeywords)2;
            public const EventKeywords Diagnostic = (EventKeywords)4;
            public const EventKeywords Perf = (EventKeywords)8;
        }

        public class Tasks
        {
            public const EventTask Page = (EventTask)1;
            public const EventTask DBQuery = (EventTask)2;
        }

        [Event(1, Message = "Application Failure: {0}", Level = EventLevel.Error, Keywords = Keywords.Diagnostic)]
        public void Failure(string message) { WriteEvent(1, message); }

        [Event(2, Message = "Starting up.", Keywords = Keywords.Perf, Level = EventLevel.Informational)]
        public void Startup() { WriteEvent(2); }

        [Event(3, Message = "loading page {1} activityID={0}", Opcode = EventOpcode.Start, 
            Task = Tasks.Page, Keywords = Keywords.Page, Level = EventLevel.Informational)]
        public void PageStart(int ID, string url) { if (IsEnabled()) WriteEvent(3, ID, url); }

        [Event(4, Opcode = EventOpcode.Stop, Task = Tasks.Page, Keywords = Keywords.Page, Level = EventLevel.Informational)]
        public void PageStop(int ID) { if (IsEnabled()) WriteEvent(4, ID); }

        [Event(5, Opcode = EventOpcode.Start, Task = Tasks.DBQuery, Keywords = Keywords.DataBase, Level = EventLevel.Informational)]
        public void DBQueryStart(string sqlQuery) { WriteEvent(5, sqlQuery); }

        [Event(6, Opcode = EventOpcode.Stop, Task = Tasks.DBQuery, Keywords = Keywords.DataBase, Level = EventLevel.Informational)]
        public void DBQueryStop() { WriteEvent(6); }

        [Event(7, Level = EventLevel.Verbose, Keywords = Keywords.DataBase)]
        public void Mark(int ID) { if (IsEnabled()) WriteEvent(7, ID); }

        [Event(8)]
        public void LogColor(MyColor color) { WriteEvent(8, (int) color); }

        public static MyCompanyEventSource Log = new MyCompanyEventSource();
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyCompanyEventSource.Log.Startup();
            Console.WriteLine("Starting up");

            MyCompanyEventSource.Log.DBQueryStart("Select * from MYTable");
            var url = "http://localhost";
            for (int i = 0; i < 10; i++)
            {
                MyCompanyEventSource.Log.PageStart(i, url);
                MyCompanyEventSource.Log.Mark(i);
                MyCompanyEventSource.Log.PageStop(i);
            }
            MyCompanyEventSource.Log.DBQueryStop();
            MyCompanyEventSource.Log.LogColor(MyColor.Blue);

            MyCompanyEventSource.Log.Failure("This is a failure 1");
            MyCompanyEventSource.Log.Failure("This is a failure 2");
            MyCompanyEventSource.Log.Failure("This is a failure 3");
        }
    }
}

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1

This type is thread safe.

Return to top
Show:
© 2016 Microsoft