Skip to content

Commit

Permalink
Remove the AddDefaultTraceListener
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Willey committed Dec 9, 2021
1 parent be71736 commit 1642ecc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 47 deletions.
30 changes: 2 additions & 28 deletions Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,35 +1046,9 @@ public virtual Task<ResponseMessage> CreateDatabaseStreamAsync(
}

/// <summary>
/// The DefaultTraceListener is removed from the CosmosClient's TraceSource unless
/// a debugger is attached.This prevents possible lock contention which can lead to
/// availability issues by requests waiting on the locks.
/// Removes the DefaultTraceListener which causes locking issues which leads to avability problems.
/// </summary>
/// <remarks>
/// This is enabled by default when Debugger.IsAttached is true. This makes it
/// easier to troubleshoot issues while debugging in Visual Studio.
/// </remarks>
public static void AddDefaultTraceListener()
{
lock (CosmosClient.defaultTraceLockObject)
{
if (Core.Trace.DefaultTrace.TraceSource.Listeners.Count > 0)
{
foreach (object traceListnerObject in Core.Trace.DefaultTrace.TraceSource.Listeners)
{
// The TraceSource already has the default trace listener
if (traceListnerObject is DefaultTraceListener)
{
return;
}
}
}

Microsoft.Azure.Cosmos.Core.Trace.DefaultTrace.TraceSource.Listeners.Add(new DefaultTraceListener());
}
}

internal static void RemoveDefaultTraceListener()
private static void RemoveDefaultTraceListener()
{
lock (CosmosClient.defaultTraceLockObject)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,24 @@ namespace Microsoft.Azure.Cosmos.Tests
public class DefaultTracingTests
{
[TestMethod]
public void DefaultTracingDisabledByDefault()
public void DefaultTracingEnableTest()
{
// Used to just force the CosmosClient static ctor to get called
// Access cosmos client to cause the static consturctor to get called
Assert.IsTrue(CosmosClient.numberOfClientsCreated >= 0);

if (Debugger.IsAttached)
{
Assert.IsTrue(this.DefaultTraceHasDefaulTraceListener());
}
else
if (!Debugger.IsAttached)
{
Assert.IsFalse(this.DefaultTraceHasDefaulTraceListener());
Assert.IsFalse(this.DefaultTraceHasDefaultTraceListener());
DefaultTrace.TraceSource.Listeners.Add(new DefaultTraceListener());
}

Assert.IsTrue(this.DefaultTraceHasDefaultTraceListener());
typeof(CosmosClient).GetMethod("RemoveDefaultTraceListener", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, null);
//CosmosClient.RemoveDefaultTraceListener();
Assert.IsFalse(this.DefaultTraceHasDefaultTraceListener());
}

[TestMethod]
public void DefaultTracingEnableTest()
{
Assert.IsFalse(this.DefaultTraceHasDefaulTraceListener());
CosmosClient.AddDefaultTraceListener();
Assert.IsTrue(this.DefaultTraceHasDefaulTraceListener());
CosmosClient.RemoveDefaultTraceListener();
Assert.IsFalse(this.DefaultTraceHasDefaulTraceListener());
}

private bool DefaultTraceHasDefaulTraceListener()
private bool DefaultTraceHasDefaultTraceListener()
{
if (DefaultTrace.TraceSource.Listeners.Count == 0)
{
Expand Down

0 comments on commit 1642ecc

Please sign in to comment.