コード例 #1
0
        private void StartTracing()
        {
            const uint RealTime = 0x00000100;
            const uint EventRecord = 0x10000000;
            const uint BufferSize = 64;
            const uint MinBuffers = 20;
            const uint MaxBuffers = 200;
            const uint FlushTimerSeconds = 1;
            int status;

            if (!LoadExistingEventTraceProperties()) {
                this.eventTraceProperties.SetParameters(RealTime, BufferSize, MinBuffers, MaxBuffers, FlushTimerSeconds);

                // Start trace session
                ulong unsafeSessionHandle;
                status = NativeMethods.StartTrace(out unsafeSessionHandle, this.loggerName, ref this.eventTraceProperties);
                if (status != 0) {
                    throw new System.ComponentModel.Win32Exception(status);
                }
                this.sessionHandle = new SessionSafeHandle(unsafeSessionHandle, this.loggerName);

                Guid EmptyGuid = Guid.Empty;

                Version Windows7Version = new Version(6, 1, 7600);
                if (Environment.OSVersion.Version.CompareTo(Windows7Version) >= 0) {
                    const int TimeToWaitForInitialize = 10 * 1000;
                    EnableTraceParameters enableParameters = new EnableTraceParameters();
                    enableParameters.Version = 1; // ENABLE_TRACE_PARAMETERS_VERSION
                    enableParameters.EnableProperty = EventEnableProperty.Sid;
                    status = NativeMethods.EnableTraceEx2(
                                unsafeSessionHandle,
                                ref this.eventProviderId,
                                1, // controlCode - EVENT_CONTROL_CODE_ENABLE_PROVIDER
                                (byte)this.Level,
                                this.MatchAnyKeyword,
                                0, // matchAnyKeyword
                                TimeToWaitForInitialize,
                                ref enableParameters);
                }
                else {
                    status = NativeMethods.EnableTraceEx(
                                ref this.eventProviderId,
                                ref EmptyGuid,          // sourceId
                                unsafeSessionHandle,
                                1,                      // isEnabled
                                (byte)this.Level,
                                this.MatchAnyKeyword,
                                0,                      // matchAllKeywords
                                EventEnableProperty.Sid,
                                IntPtr.Zero);
                }
                if (status != 0) {
                    throw new System.ComponentModel.Win32Exception(status);
                }
            }

            this.logFile = new EventTraceLogfile();
            this.logFile.LoggerName = this.loggerName;
            this.logFile.EventRecordCallback = EventRecordCallback;

            this.logFile.ProcessTraceMode = EventRecord | RealTime;
            ulong unsafeTraceHandle = NativeMethods.OpenTrace(ref this.logFile);
            status = Marshal.GetLastWin32Error();
            if (status != 0) {
                throw new System.ComponentModel.Win32Exception(status);
            }
            this.traceHandle = new TraceSafeHandle(unsafeTraceHandle);

            this.processEventsDelgate = new ProcessTraceDelegate(ProcessTraceInBackground);
            this.asyncResult = this.processEventsDelgate.BeginInvoke(this.traceHandle, null, this.processEventsDelgate);
        }
コード例 #2
0
 internal static extern ulong /*session handle*/ OpenTrace(
     [In, Out]
     ref EventTraceLogfile logfile);