Skip to content

Commit

Permalink
Don't initialize trace if specifically asked not to (#1490)
Browse files Browse the repository at this point in the history
* Donot initialize trace if specifically asked not to

* For Testhost only Initialize Tracing if --diag is passed

* test fix

* Tracing for datacollector also to be initialized via --diag switch
  • Loading branch information
mayankbansal018 authored Mar 26, 2018
1 parent abfc9ca commit eca43d7
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 17 deletions.
12 changes: 12 additions & 0 deletions src/Microsoft.TestPlatform.CoreUtilities/Tracing/EqtTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public static string LogFile
}
}

public static bool DoNotInitailize
{
get
{
return traceImpl.DoNotInitialize;
}
set
{
traceImpl.DoNotInitialize = value;
}
}

public static string ErrorOnInitialization
{
get;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ namespace Microsoft.VisualStudio.TestPlatform.ObjectModel
/// </summary>
public partial interface IPlatformEqtTrace
{
// This is added to ensure that tracing for testhost/datacollector should not be instantiated if not enabled via --diag switch.
bool DoNotInitialize
{
get;
set;
}

/// <summary>
/// Adds the message to the trace log.
/// The line becomes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static string ErrorOnInitialization
}

// This is added to ensure that traceSource should not be instantiated in when creating appdomains if EqtTrace is not enabled.
internal static bool DoNotInitialize
public bool DoNotInitialize
{
get;
set;
Expand Down Expand Up @@ -175,7 +175,7 @@ public bool InitializeVerboseTrace(string customLogFile)
/// <inheritdoc/>
public bool ShouldTrace(PlatformTraceLevel traceLevel)
{
if (DoNotInitialize)
if (this.DoNotInitialize)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void SetupRemoteEqtTraceListeners(AppDomain childDomain)
}
else
{
remoteEqtTrace.DoNotInitialize = true;
this.DoNotInitialize = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ public TraceLevel TraceLevel
}
}

/// <summary>
/// This is added to ensure that traceSource should not be instantiated in when creating appdomains if EqtTrace is not enabled.
/// </summary>
internal bool DoNotInitialize
{
set
{
PlatformEqtTrace.DoNotInitialize = value;
}
}

/// <summary>
/// Register listeners from parent domain in current domain.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class PlatformEqtTrace : IPlatformEqtTrace
{
public static string ErrorOnInitialization { get; set; }

public bool DoNotInitialize
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

public void WriteLine(PlatformTraceLevel level, string message)
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class PlatformEqtTrace : IPlatformEqtTrace

public static string LogFile { get; set; }

public bool DoNotInitialize
{
get;
set;
}

private static PlatformTraceLevel TraceLevel { get; set; }

/// <inheritdoc/>
Expand Down
4 changes: 4 additions & 0 deletions src/datacollector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ private static void Run(string[] args)
{
EqtTrace.InitializeVerboseTrace(logFile);
}
else
{
EqtTrace.DoNotInitailize = true;
}

// Attach to exit of parent process
var parentProcessId = CommandLineArgumentsHelper.GetIntArgFromDict(argsDictionary, ParentProcessArgument);
Expand Down
8 changes: 5 additions & 3 deletions src/testhost.x86/DefaultEngineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ internal class DefaultEngineInvoker :
public void Invoke(IDictionary<string, string> argsDictionary)
{
// Setup logging if enabled
string logFile;
if (argsDictionary.TryGetValue(LogFileArgument, out logFile))
if (argsDictionary.TryGetValue(LogFileArgument, out string logFile))
{
EqtTrace.InitializeVerboseTrace(logFile);
}

else
{
EqtTrace.DoNotInitailize = true;
}
#if NET451
if (EqtTrace.IsInfoEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class EqtTraceTests
[ClassInitialize]
public static void Init(TestContext testContext)
{
// Set DoNotInitailize to false.
EqtTrace.DoNotInitailize = false;
dirPath = Path.Combine(Path.GetTempPath(), "TraceUT");
try
{
Expand Down Expand Up @@ -119,6 +121,7 @@ public void TraceShouldWriteVerbose()
EqtTrace.Verbose("Dummy Verbose Message");
Assert.IsTrue(ReadLogFile().Contains("Dummy Verbose Message"), "Expected Verbose message");
}

[TestMethod]
public void TraceShouldWriteInfo()
{
Expand All @@ -131,6 +134,19 @@ public void TraceShouldWriteInfo()
Assert.IsTrue(ReadLogFile().Contains("Dummy Info Message"), "Expected Info message");
}

[TestMethod]
public void TraceShouldNotWriteIfDoNotInitializationIsSetToTrue()
{
EqtTrace.DoNotInitailize = true;
#if NET451
EqtTrace.TraceLevel = TraceLevel.Info;
#else
EqtTrace.TraceLevel = PlatformTraceLevel.Info;
#endif
EqtTrace.Info("Dummy Info Message: TraceShouldNotWriteIfDoNotInitializationIsSetToTrue");
Assert.IsTrue(!ReadLogFile().Contains("Dummy Info Message: TraceShouldNotWriteIfDoNotInitializationIsSetToTrue"), "Did not expect Dummy Info message");
}

private string ReadLogFile()
{
string log = null;
Expand Down

0 comments on commit eca43d7

Please sign in to comment.