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();
}
}
}
}
/// <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); } }