Example #1
0
        private static int SubscribeAndProcessCloudBuildEvents()
        {
            if (!(TraceEventSession.IsElevated() ?? false))
            {
                Error("Not elevated; exiting");
                return(-1);
            }

            // BuildXL.Tracing.ETWLogger guid
            if (!Guid.TryParse("43b71382-88db-5427-89d5-0b46476f8ef4", out Guid guid))
            {
                Error("Could not parse guid; exiting");
                return(-1);
            }

            Console.WriteLine("Listening for cloud build events");

            // Create an unique session
            string sessionName = "DropDaemon ETW Session";

            // the null second parameter means 'real time session'
            using (TraceEventSession traceEventSession = new TraceEventSession(sessionName, null))
            {
                // Note that sessions create a OS object (a session) that lives beyond the lifetime of the process
                // that created it (like Files), thus you have to be more careful about always cleaning them up.
                // An importantly way you can do this is to set the 'StopOnDispose' property which will cause
                // the session to
                // stop (and thus the OS object will die) when the TraceEventSession dies.   Because we used a 'using'
                // statement, this means that any exception in the code below will clean up the OS object.
                traceEventSession.StopOnDispose = true;
                traceEventSession.EnableProvider(guid, matchAnyKeywords: (ulong)Keywords.CloudBuild);

                // Prepare to read from the session, connect the ETWTraceEventSource to the session
                using (ETWTraceEventSource etwTraceEventSource = new ETWTraceEventSource(
                           sessionName,
                           TraceEventSourceType.Session))
                {
                    DynamicTraceEventParser dynamicTraceEventParser = new DynamicTraceEventParser(etwTraceEventSource);
                    dynamicTraceEventParser.All += traceEvent =>
                    {
                        Possible <CloudBuildEvent, Failure> possibleEvent = CloudBuildEvent.TryParse(traceEvent);

                        if (!possibleEvent.Succeeded)
                        {
                            Error(possibleEvent.Failure.ToString());
                            return;
                        }

                        CloudBuildEvent eventObj = possibleEvent.Result;
                        Console.WriteLine("*** Event received: " + eventObj.Kind);
                    };

                    etwTraceEventSource.Process();
                }
            }

            Console.WriteLine("FINISHED");
            Thread.Sleep(100000);
            return(0);
        }
Example #2
0
        /// <inheritdoc />
        protected override void OnEventWritten(EventWrittenEventArgs eventData)
        {
            var ev = CloudBuildEvent.TryParse(
                eventData.EventName,
                eventData.Payload);

            Contract.Assert(ev.Succeeded, "expected to be able to parse received ETW event into CloudBuildEvent");
            m_receivedEvents.Enqueue(ev.Result);
        }