public override void Dispose() { base.Dispose(); foreach (var childListener in m_childListeners) { childListener.Dispose(); } TestEventListenerBase existingListener = Interlocked.CompareExchange(ref s_currentTestListener, null, comparand: this); AssertTrue(existingListener == this, "TestEventListener should not be disposed after a new one has been registered (concurrent tests?)"); GC.SuppressFinalize(this); }
/// <summary> /// Creates an instance attached to a particular named test. /// </summary> /// <param name="eventSource"> /// The event source to listen to. /// </param> /// <param name="fullyQualifiedTestName"> /// Name of the owning test. Used for diagnostics when these listeners leak (not disposed after a test execution). /// </param> /// <param name="captureAllDiagnosticMessages"> /// If true, all messages tagged with Diagnostics are captured (rather than needing to be enabled per-task). /// </param> /// <param name="logAction"> /// Action to perform when logging a string. This allows test frameworks to hook into their own logging. /// Writes to the console if unspecified /// </param> /// <param name="eventMask">event mask for controlling which events are enabled/disabled</param> protected TestEventListenerBase(Events eventSource, string fullyQualifiedTestName, bool captureAllDiagnosticMessages = true, Action <string> logAction = null, EventMask eventMask = null) : base(eventSource, null, EventLevel.Verbose, captureAllDiagnosticMessages: captureAllDiagnosticMessages, listenDiagnosticMessages: true, eventMask: eventMask) { Contract.Requires(eventSource != null); Contract.Requires(!string.IsNullOrEmpty(fullyQualifiedTestName)); m_owningTestFullyQualifiedName = fullyQualifiedTestName; m_logAction = logAction; TestEventListenerBase existingListener = Interlocked.Exchange(ref s_currentTestListener, this); if (existingListener != null) { Interlocked.CompareExchange(ref s_currentTestListener, null, comparand: this); #pragma warning disable CA2214 // Do not call overridable methods in constructors AssertTrue( false, "A TestEventListener for {0} was not disposed upon completion of the test. This can cause repeated log messages and impact test performance.", existingListener.m_owningTestFullyQualifiedName); #pragma warning restore CA2214 // Do not call overridable methods in constructors } }