Endjin.Assembly.ChangeDetection.Infrastructure.TracerConfig.Dispose C# (CSharp) Method

Dispose() public method

Close the current active trace listeners in a thread safe way.
public Dispose ( ) : void
return void
        public void Dispose()
        {
            lock (myLock)
            {
                // The shutdown protocol works like this
                // 1. Get all listeners into an array
                // 2. Clear the thread safe listeners collection
                // 3. Call flush and dispose on each listener to ensure that any pending messages are written.
                // This way we ensure that while we are shutting the listerns down no additional trace messages
                // arrive which could be used accidentally by tracing. Using a disposed listener is almost always a bad idea.
                if (this.myListeners != null && this.myListeners.Count > 0)
                {
                    var listeners = new TraceListener[this.myListeners.Count];
                    this.myListeners.CopyTo(listeners, 0);
                    this.myListeners.Clear();
                    foreach (var listener in listeners)
                    {
                        listener.Flush();
                        listener.Dispose();
                    }
                }
            }
        }

Usage Example

Beispiel #1
0
 /// <summary>
 ///     Re/Set trace configuration in a thread safe way by shutting down the already existing listeners and then
 ///     put the new config into place.
 /// </summary>
 /// <param name="cfg">
 ///     The trace string format is of the form OutputDevice;TypeFilter MessageFilter; TypeFilter
 ///     MessageFilter; ...
 /// </param>
 /// <param name="bClearEvents">if true all registered trace callbacks are removed.</param>
 /// <returns>The old trace configuration string.</returns>
 public static string Reset(string cfg, bool bClearEvents)
 {
     lock (myLock)
     {
         Instance.Dispose();
         var old = Environment.GetEnvironmentVariable(TraceEnvVarName);
         Environment.SetEnvironmentVariable(TraceEnvVarName, cfg);
         Instance = new TracerConfig(Environment.GetEnvironmentVariable(TraceEnvVarName));
         if (bClearEvents)
         {
             Tracer.ClearEvents();
         }
         return(old);
     }
 }