1 out of 3 rated this helpful - Rate this topic

EventSource Class

.NET Framework 4.5

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

System.Object
  System.Diagnostics.Tracing.EventSource

Namespace:  System.Diagnostics.Tracing
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Class EventSource _
	Implements IDisposable

The EventSource type exposes the following members.

  NameDescription
Protected methodSupported in .NET for Windows Store appsEventSourceCreates a new instance of the EventSource class.
Protected methodSupported in .NET for Windows Store appsEventSource(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.
Top
  NameDescription
Public propertyConstructionException [This topic is preliminary and is subject to change.]
Gets any exception that was thrown during the construction of the event source.
Public propertyStatic memberCurrentThreadActivityId [This topic is preliminary and is subject to change.]
Gets the activity ID of the current thread.
Public propertySupported in .NET for Windows Store appsGuidThe unique identifier for the event source.
Public propertySupported in .NET for Windows Store appsNameThe friendly name of the class that is derived from the event source.
Top
  NameDescription
Public methodSupported in .NET for Windows Store appsDisposeReleases all resources used by the current instance of the EventSource class.
Protected methodSupported in .NET for Windows Store appsDispose(Boolean)Releases the unmanaged resources used by the EventSource class and optionally releases the managed resources.
Public methodSupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported in .NET for Windows Store appsFinalizeAllows the EventSource object to attempt to free resources and perform other cleanup operations before the object is reclaimed by garbage collection. (Overrides Object.Finalize.)
Public methodStatic memberSupported in .NET for Windows Store appsGenerateManifestReturns a string of the XML manifest that is associated with the current event source.
Public methodStatic memberSupported in .NET for Windows Store appsGetGuidGets the unique identifier for this implementation of the event source.
Public methodSupported in .NET for Windows Store appsGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodStatic memberSupported in .NET for Windows Store appsGetNameGets the friendly name of the event source.
Public methodStatic memberSupported in .NET for Windows Store appsGetSourcesGets a snapshot of all the event sources for the application domain.
Public methodSupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported in .NET for Windows Store appsIsEnabledDetermines whether the current event source is enabled.
Public methodSupported in .NET for Windows Store appsIsEnabled(EventLevel, EventKeywords)Determines whether the current event source that has the specified level and keyword is enabled.
Protected methodSupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported in .NET for Windows Store appsOnEventCommandCalled when the current event source is updated by the controller.
Public methodStatic memberSupported in .NET for Windows Store appsSendCommandSends a command to a specified event source.
Public methodStatic memberSetCurrentThreadActivityId(Guid) [This topic is preliminary and is subject to change.]
Sets the activity ID on the current thread.
Public methodStatic memberSetCurrentThreadActivityId(Guid, Guid) [This topic is preliminary and is subject to change.]
Sets the activity ID on the current thread, and returns the previous activity ID.
Public methodSupported in .NET for Windows Store appsToStringObtains a string representation of the current event source instance. (Overrides Object.ToString.)
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32)Writes an event by using the provided event identifier.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Int32)Writes an event by using the provided event identifier and 32-bit integer argument.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Int64)Writes an event by using the provided event identifier and 64-bit integer argument.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Object())Writes an event by using the provided event identifier and array of arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, String)Writes an event by using the provided event identifier and string argument.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Int32, Int32)Writes an event by using the provided event identifier and 32-bit integer arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Int64, Int64)Writes an event by using the provided event identifier and 64-bit arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, String, Int32)Writes an event by using the provided event identifier and arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, String, Int64)Writes an event by using the provided event identifier and arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, String, String)Writes an event by using the provided event identifier and string arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Int32, Int32, Int32)Writes an event by using the provided event identifier and 32-bit integer arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, Int64, Int64, Int64)Writes an event by using the provided event identifier and 64-bit arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, String, Int32, Int32)Writes an event by using the provided event identifier and arguments.
Protected methodSupported in .NET for Windows Store appsWriteEvent(Int32, String, String, String)Writes an event by using the provided event identifier and string arguments.
Protected methodSupported in .NET for Windows Store appsWriteEventCoreCreates a new WriteEvent overload by using the provided event identifier and event data.
Protected methodWriteEventWithRelatedActivityId [This topic is preliminary and is subject to change.]
Writes an event that indicates that the current activity is related to another activity.
Protected methodWriteEventWithRelatedActivityIdCore [This topic is preliminary and is subject to change.]
Writes an event that indicates that the current activity is related to another activity.
Top

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.

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.

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(1, EventTask)
    End Class 'Tasks

    <[Event](1, Message:="Application Falure: {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

.NET Framework

Supported in: 4.5

.NET for Windows Store apps

Supported in: Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.