windows - ETW Utilsigtet Trace Event 254?

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg arbejder med at oprette en WCF-tjeneste, der er vært i en Windows-tjeneste, der bruger Event Tracing for Windows (ETW) til logning. I øjeblikket fokuserer jeg kun på at oprette en dummy Windows Service med ETW, og jeg ser nogle Utilsigtede begivenheder bliver logget. I særdeleshed, når jeg starter eller stopper tjenesten, ser jeg en begivenhed med Opcode 254 og ingen besked.


Jeg har prøvet at kigge på nettet, men hidtil har jeg ikke fundet nogen henvisninger til, hvad dette er.


Hvis du vil prøve det, er servicekoden:


Imports System.Threading

Public Class MyTestService

    Private \_cts As CancellationTokenSource
    Private \_workTask As Task = Nothing

    Private Class WorkState
        Public Property CancellationToken As CancellationToken
        Public Sub New(ByVal cancelToken As CancellationToken)
            Me.CancellationToken = cancelToken
        End Sub

    End Class

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.

        Debugger.Launch()

        \_cts = New CancellationTokenSource()
        \_workTask = Task.Factory.StartNew(AddressOf DoWork, New WorkState(\_cts.Token), \_cts.Token)
        \_workTask.ContinueWith(AddressOf OnWorkDone)

        MyTestServiceEventSource.Log.OnServiceStarted()
    End Sub

    Private Sub OnWorkDone(parent As Task)
        If Not IsNothing(parent.Exception) Then
            MyTestServiceEventSource.Log.OnError(parent.Exception)
        End If
    End Sub

    Private Sub DoWork(ByVal obj As Object)
        Dim state = CType(obj, WorkState)

        While Not state.CancellationToken.IsCancellationRequested
            Threading.Thread.Sleep(5000)
            MyTestServiceEventSource.Log.OnTick()
        End While

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.

        Try
            \_cts.Cancel()
            \_workTask.Wait(1500)
        Catch ex As Exception
            MyTestServiceEventSource.Log.OnError(ex)
        Finally
            \_cts = Nothing
            \_workTask = Nothing
        End Try

        MyTestServiceEventSource.Log.OnServiceStopped()
    End Sub

End Class


Og her er Event Source-klassen:


Imports System.Diagnostics.Tracing
Imports Newtonsoft

<EventSource(Name:="Company-MyTestService")>
Public Class MyTestServiceEventSource
    Inherits EventSource

#Region "Singleton Pattern"
    Private Shared \_inst As New Lazy(Of MyTestServiceEventSource)(Function() New MyTestServiceEventSource())
    Public Shared ReadOnly Property Log As MyTestServiceEventSource
        Get
            Return \_inst.Value
        End Get
    End Property

    Private Sub New()
        MyBase.New()
    End Sub
#End Region

    Public Class EventIds
        Public Const SERVICE\_STARTED As Integer = 1
        Public Const SERVICE\_STOPPPED As Integer = 2
        Public Const TICK\_EVENT As Integer = 3
        Public Const ERROR\_EVENT As Integer = 4
    End Class

    <[Event](EventIds.SERVICE\_STARTED, Opcode:=EventOpcode.Start, Message:="MyTestService Started", Level:=EventLevel.Informational)>
    Public Sub OnServiceStarted()
        If IsEnabled() Then
            Me.WriteEvent(EventIds.SERVICE\_STARTED)
        End If
    End Sub


    <[Event](EventIds.TICK\_EVENT, Message:="MyTestService Tick", Level:=EventLevel.Verbose)>
    Public Sub OnTick()
        If IsEnabled() Then
            Me.WriteEvent(EventIds.TICK\_EVENT)
        End If
    End Sub

    <[Event](EventIds.ERROR\_EVENT, Message:="{0}", Level:=EventLevel.Error)>
    Public Sub OnError(ByVal errMsg As String, ByVal details As String)
        If IsEnabled() Then
            Me.WriteEvent(EventIds.ERROR\_EVENT, errMsg, details)
        End If
    End Sub

    <NonEvent>
    Public Sub OnError(ByVal ex As Exception)
        Try
            Dim details = Json.JsonConvert.SerializeObject(ex)
            OnError(ex.Message, details)
        Catch ex2 As Exception
            Debugger.Break()
        End Try
    End Sub

    <[Event](EventIds.SERVICE\_STOPPPED, Opcode:=EventOpcode.Stop, Message:="MyTestService Stopped", Level:=EventLevel.Informational)>
    Public Sub OnServiceStopped()
        If IsEnabled() Then
            Me.WriteEvent(EventIds.SERVICE\_STOPPPED)
        End If
    End Sub



End Class

Bedste reference


Alle ETW-udbydere skal have et manifest, der viser udbyderbegivenhederne. Dette gøres af EventSource via ManifestData Eventen som du ser. [4]


Event MSec='16059,4211' PID='3444' PName='foo' TID='1776' ActivityID='ffd679657c475d357c32b8dbe608b3ff' EventName='ManifestData' ProviderName='FooEventSource' ProviderGuid='GUID' ClassicProvider='False' ProcessorNumber='1' Opcode='254' >