3

我很困惑如何EventListener为现有系统 ETW Provider 创建一个。该EnableEvents方法期望在EventSource实例中传递。我需要手动创建吗?有没有办法生成那个EventSource类?

我可以找到提供者的详细信息:

logman query providers Microsoft-Windows-WinHttp
wevtutil get-publisher Microsoft-Windows-WinHttp

我什至可以instrumentationManifest使用以下命令生成 xml:

.\PerfView.exe /nogui userCommand DumpRegisteredManifest Microsoft-Windows-WinHttp
open System.Diagnostics.Tracing

let printfn format = Printf.ksprintf System.Diagnostics.Debug.WriteLine format

[<EventSource(Name="Microsoft-Windows-NDIS-PacketCapture", Guid="2ed6006e-4729-4609-b423-3ee7bcd678ef")>]
type NdisPacketCaptureEventSource() =
    inherit EventSource()

let Ethernet = LanguagePrimitives.EnumOfValue<int64, EventKeywords>(0x1L)
let WirelessWAN = LanguagePrimitives.EnumOfValue<int64, EventKeywords>(0x200L)

let Api = LanguagePrimitives.EnumOfValue<int64, EventKeywords>(0x1L)
let Send = LanguagePrimitives.EnumOfValue<int64, EventKeywords>(0x100000000L)

[<EventSource(Name="Microsoft-Windows-WinHttp", Guid="7d44233d-3055-4b9c-ba64-0d47ca40a232")>]
type WinHttpEventSource() =
    inherit EventSource()

type MyEventListener() =
    inherit EventListener()

    override x.OnEventSourceCreated source =
        printfn "OnEventSourceCreated %A" source
        base.OnEventSourceCreated source

    override x.OnEventWritten args =
        printfn "OnEventWritten %A" args
        base.OnEventWritten args

[<EntryPoint>]
let main argv = 
    use listener = new MyEventListener()

//    use ndis = new NdisPacketCaptureEventSource()
//    printfn "Name: %s, Guid: %A" ndis.Name ndis.Guid
//    listener.EnableEvents(ndis, EventLevel.Verbose, EventKeywords.All)
//    listener.EnableEvents(ndis, EventLevel.Verbose, Ethernet)
//    listener.EnableEvents(ndis, EventLevel.Verbose, WirelessWAN)

    use winHttp = new WinHttpEventSource()
//    listener.EnableEvents(winHttp, EventLevel.Verbose, EventKeywords.All)
    listener.EnableEvents(winHttp, EventLevel.Verbose, Send)

//    for i in 0 .. 5 do
//    System.Console.ReadKey() |> ignore
    while true do
        System.Threading.Thread.Sleep 100
    0

输出

我一直无法触发OnEventWritten,我不知道为什么。有任何想法吗?

2015-12-02 更新

我能够EventSource使用 SemanticLogging 获取事件记录。详情在这里。它正在使用Microsoft.Diagnostics.Tracing.TraceEvent。使用它似乎可能是解决方案。

4

1 回答 1

3

我不太明白我在问什么。我想记录来自 ETL 的事件。在 Vance Morrison 的博客上,他解释 EventSource keeps these track of these data item names and types so that processors (either EventListeners or ETW)道。我尝试使用的 EventListener 用于同一进程中的事件。TraceEvent可用于从另一个进程获取事件。他提供的样品很棒,我用它们来制作这个。

open System

// https://www.nuget.org/packages/Microsoft.Diagnostics.Tracing.TraceEvent/
open Microsoft.Diagnostics.Tracing
open Microsoft.Diagnostics.Tracing.Session

// toggle this on for VS Output window or off for Console window
let printfn format = Printf.ksprintf System.Diagnostics.Debug.WriteLine format

[<EntryPoint>]
let main argv = 
    
    if not (TraceEventSession.IsElevated().GetValueOrDefault()) then
        printfn "you must run as admin"
        1

    else 
        let name = "Microsoft-AdoNet-SystemData"
        let guid = TraceEventProviders.GetProviderGuidByName name
        printfn "name: %s, guid: %A" name guid
        use sn = new TraceEventSession("MySession")
        sn.Source.add_AllEvents (fun evt -> printfn "event: %A" evt)
        sn.EnableProvider(name, TraceEventLevel.Verbose) |> ignore
        Console.CancelKeyPress.Add (fun args -> args.Cancel <- true; sn.Dispose())
        sn.Source.Process() |> ignore
        0

在此处输入图像描述

于 2015-12-03T20:40:23.890 回答