EventSource Class
Provides the ability to create events for event tracing for Windows (ETW).
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | EventSource() | Creates a new instance of the EventSource class. |
![]() | EventSource(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. |
![]() | EventSource(EventSourceSettings) | Creates a new instance of the EventSource class with the specified configuration settings. |
![]() | EventSource(EventSourceSettings, String()) | Initializes a new instance of the EventSource to be used with non-contract events that contains the specified settings and traits. |
![]() | EventSource(String) | Creates a new instance of the EventSource class with the specified name. |
![]() | EventSource(String, EventSourceSettings) | Creates a new instance of the EventSource class with the specified name and settings. |
![]() | EventSource(String, EventSourceSettings, String()) | Creates a new instance of the EventSource class with the specified configuration settings. |
| Name | Description | |
|---|---|---|
![]() | ConstructionException | [Supported in the .NET Framework 4.5.1 and later versions] Gets any exception that was thrown during the construction of the event source. |
![]() ![]() | CurrentThreadActivityId | [Supported in the .NET Framework 4.5.1 and later versions] Gets the activity ID of the current thread. |
![]() | Guid | The unique identifier for the event source. |
![]() | Name | The friendly name of the class that is derived from the event source. |
![]() | Settings | Gets the settings applied to this event source. |
| Name | Description | |
|---|---|---|
![]() | Dispose() | Releases all resources used by the current instance of the EventSource class. |
![]() | Dispose(Boolean) | Releases the unmanaged resources used by the EventSource class and optionally releases the managed resources. |
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | 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().) |
![]() ![]() | GenerateManifest(Type, String) | Returns a string of the XML manifest that is associated with the current event source. |
![]() ![]() | GenerateManifest(Type, String, EventManifestOptions) | Returns a string of the XML manifest that is associated with the current event source. |
![]() ![]() | GetGuid(Type) | Gets the unique identifier for this implementation of the event source. |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() ![]() | GetName(Type) | Gets the friendly name of the event source. |
![]() ![]() | GetSources() | Gets a snapshot of all the event sources for the application domain. |
![]() | GetTrait(String) | Gets the trait value associated with the specified key. |
![]() | GetType() | |
![]() | IsEnabled() | Determines whether the current event source is enabled. |
![]() | IsEnabled(EventLevel, EventKeywords) | Determines whether the current event source that has the specified level and keyword is enabled. |
![]() | IsEnabled(EventLevel, EventKeywords, EventChannel) | Determines whether the current event source is enabled for events with the specified level, keywords and channel. |
![]() | MemberwiseClone() | |
![]() | OnEventCommand(EventCommandEventArgs) | Called when the current event source is updated by the controller. |
![]() ![]() | SendCommand(EventSource, EventCommand, IDictionary(Of String, String)) | Sends a command to a specified event source. |
![]() ![]() | SetCurrentThreadActivityId(Guid) | [Supported in the .NET Framework 4.5.1 and later versions] Sets the activity ID on the current thread. |
![]() ![]() | SetCurrentThreadActivityId(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. |
![]() | ToString() | Obtains a string representation of the current event source instance.(Overrides Object.ToString().) |
![]() | Write(String) | Writes an event without fields, but with the specified name and default options. |
![]() | Write(String, EventSourceOptions) | Writes an event without fields, but with the specified name and options. |
![]() | Write(Of T)(String, T) | Writes an event with the specified name and data. |
![]() | Write(Of T)(String, EventSourceOptions, T) | Writes an event with the specified name, event data and options. |
![]() | Write(Of T)(String, EventSourceOptions, T) | Writes an event with the specified name, options and event data. |
![]() | Write(Of T)(String, EventSourceOptions, Guid, Guid, T) | Writes an event with the specified name, options, related activity and event data. |
![]() | WriteEvent(Int32) | Writes an event by using the provided event identifier. |
![]() | WriteEvent(Int32, Byte()) | Writes an event by using the provided event identifier and byte array argument. |
![]() | WriteEvent(Int32, Int32) | Writes an event by using the provided event identifier and 32-bit integer argument. |
![]() | WriteEvent(Int32, Int32, Int32) | Writes an event by using the provided event identifier and 32-bit integer arguments. |
![]() | WriteEvent(Int32, Int32, Int32, Int32) | Writes an event by using the provided event identifier and 32-bit integer arguments. |
![]() | WriteEvent(Int32, Int32, String) | Writes an event by using the provided event identifier and 32-bit integer and string arguments. |
![]() | WriteEvent(Int32, Int64) | Writes an event by using the provided event identifier and 64-bit integer argument. |
![]() | WriteEvent(Int32, Int64, Byte()) | Writes the event data using the specified indentifier and 64-bit integer and byte array arguments. |
![]() | WriteEvent(Int32, Int64, Int64) | Writes an event by using the provided event identifier and 64-bit arguments. |
![]() | WriteEvent(Int32, Int64, Int64, Int64) | Writes an event by using the provided event identifier and 64-bit arguments. |
![]() | WriteEvent(Int32, Int64, String) | Writes an event by using the provided event identifier and 64-bit integer, and string arguments. |
![]() | WriteEvent(Int32, Object()) | Writes an event by using the provided event identifier and array of arguments. |
![]() | WriteEvent(Int32, String) | Writes an event by using the provided event identifier and string argument. |
![]() | WriteEvent(Int32, String, Int32) | Writes an event by using the provided event identifier and arguments. |
![]() | WriteEvent(Int32, String, Int32, Int32) | Writes an event by using the provided event identifier and arguments. |
![]() | WriteEvent(Int32, String, Int64) | Writes an event by using the provided event identifier and arguments. |
![]() | WriteEvent(Int32, String, String) | Writes an event by using the provided event identifier and string arguments. |
![]() | WriteEvent(Int32, String, String, String) | Writes an event by using the provided event identifier and string arguments. |
![]() | WriteEventCore(Int32, Int32, EventSource.EventData*) | Creates a new WriteEvent overload by using the provided event identifier and event data. |
![]() | WriteEventWithRelatedActivityId(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. |
![]() | WriteEventWithRelatedActivityIdCore(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. |
| Name | Description | |
|---|---|---|
![]() | EventCommandExecuted | 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.
Important |
|---|
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.
Imports System.Diagnostics.Tracing Class MyCompanyEventSource Inherits EventSource Public Shared Log As New MyCompanyEventSource() Public Sub Startup() WriteEvent(1) End Sub 'Startup Public Sub OpenFileStart(ByVal fileName As String) WriteEvent(2, fileName) End Sub 'OpenFileStart Public Sub OpenFileStop() WriteEvent(3) End Sub 'OpenFileStop End Class 'MyCompanyEventSource Class Program Shared Sub Main(ByVal args() As String) MyCompanyEventSource.Log.Startup() ' ... MyCompanyEventSource.Log.OpenFileStart("SomeFile") ' ... MyCompanyEventSource.Log.OpenFileStop() End Sub 'Main End Class 'Program
The following example shows a more complex implementation of the EventSource class.
Imports System Imports System.Diagnostics.Tracing Enum MyColor Red Yellow Blue End Enum 'MyColor <EventSource(Name:="MyCompany")> _ Class MyCompanyEventSource Inherits EventSource Public Class Keywords Public Const Page As EventKeywords = CType(1, EventKeywords) Public Const DataBase As EventKeywords = CType(2, EventKeywords) Public Const Diagnostic As EventKeywords = CType(4, EventKeywords) Public Const Perf As EventKeywords = CType(8, EventKeywords) End Class 'Keywords Public Class Tasks Public Const Page As EventTask = CType(1, EventTask) Public Const DBQuery As EventTask = CType(2, EventTask) End Class 'Tasks <[Event](1, Message:="Application Failure: {0}", Level:=EventLevel.Error, Keywords:=Keywords.Diagnostic)> _ Public Sub Failure(ByVal message As String) WriteEvent(1, message) End Sub 'Failure <[Event](2, Message:="Starting up.", Keywords:=Keywords.Perf, Level:=EventLevel.Informational)> _ Public Sub Startup() WriteEvent(2) End Sub 'Startup <[Event](3, Message:="loading page {1} activityID={0}", Opcode:=EventOpcode.Start, Task:=Tasks.Page, Keywords:=Keywords.Page, Level:=EventLevel.Informational)> _ Public Sub PageStart(ByVal ID As Integer, ByVal url As String) If IsEnabled() Then WriteEvent(3, ID, url) End If End Sub 'PageStart <[Event](4, Opcode:=EventOpcode.Stop, Task:=Tasks.Page, Keywords:=Keywords.Page, Level:=EventLevel.Informational)> _ Public Sub PageStop(ByVal ID As Integer) If IsEnabled() Then WriteEvent(4, ID) End If End Sub 'PageStop <[Event](5, Opcode:=EventOpcode.Start, Task:=Tasks.DBQuery, Keywords:=Keywords.DataBase, Level:=EventLevel.Informational)> _ Public Sub DBQueryStart(ByVal sqlQuery As String) WriteEvent(5, sqlQuery) End Sub 'DBQueryStart <[Event](6, Opcode:=EventOpcode.Stop, Task:=Tasks.DBQuery, Keywords:=Keywords.DataBase, Level:=EventLevel.Informational)> _ Public Sub DBQueryStop() WriteEvent(6) End Sub 'DBQueryStop <[Event](7, Level:=EventLevel.Verbose, Keywords:=Keywords.DataBase)> _ Public Sub Mark(ByVal ID As Integer) If IsEnabled() Then WriteEvent(7, ID) End If End Sub 'Mark <[Event](8)> _ Public Sub LogColor(ByVal color As MyColor) WriteEvent(8, Fix(color)) End Sub 'LogColor Public Shared Log As New MyCompanyEventSource() End Class 'MyCompanyEventSource Class Program Shared Sub Main(ByVal args() As String) MyCompanyEventSource.Log.Startup() Console.WriteLine("Starting up") MyCompanyEventSource.Log.DBQueryStart("Select * from MYTable") Dim url As String = "http:'localhost" Dim i As Integer For i = 0 To 9 MyCompanyEventSource.Log.PageStart(i, url) MyCompanyEventSource.Log.Mark(i) MyCompanyEventSource.Log.PageStop(i) Next 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") End Sub 'Main End Class 'Program
Available since 8
.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.





