Skip to content

Commit 458a019

Browse files
Improve in-proc collector logs (#552)
Improve in-proc collector logs
1 parent cfcb1b3 commit 458a019

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

Documentation/Troubleshooting.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,17 @@ Fire attach
199199
System.Diagnostics.Debugger.Launch();
200200
```
201201

202+
If you want debug in-process collector you need to set VSTEST_HOST_DEBUG(https://github.com/microsoft/vstest/issues/2158) environment variable
203+
```
204+
set VSTEST_HOST_DEBUG=1
205+
```
206+
Test host will wait for debugger
207+
```
208+
Starting test execution, please wait...
209+
Logging Vstest Diagnostics in file: C:\git\coverletissue\collectorlog\XUnitTestProject1\log.txt
210+
Host debugging is enabled. Please attach debugger to testhost process to continue.
211+
Process Id: 800, Name: dotnet
212+
```
213+
202214
**Every time you update code and rebuild new package remember to remove local nuget cache(`RMDIR "C:\Users\[winUser]\.nuget\packages\coverlet.collector" /S /Q`) otherwise you'll load old collector code because the package version wasn't changed**
215+

src/coverlet.collector/InProcDataCollection/CoverletInProcDataCollector.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Reflection;
3+
34
using coverlet.collector.Resources;
45
using Coverlet.Collector.Utilities;
56
using Coverlet.Core.Instrumentation;
6-
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
77
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
88
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollector.InProcDataCollector;
99
using Microsoft.VisualStudio.TestPlatform.ObjectModel.InProcDataCollector;
@@ -12,8 +12,12 @@ namespace Coverlet.Collector.DataCollection
1212
{
1313
public class CoverletInProcDataCollector : InProcDataCollection
1414
{
15+
private TestPlatformEqtTrace _eqtTrace;
16+
1517
public void Initialize(IDataCollectionSink dataCollectionSink)
1618
{
19+
_eqtTrace = new TestPlatformEqtTrace();
20+
_eqtTrace.Verbose("Initialize CoverletInProcDataCollector");
1721
}
1822

1923
public void TestCaseEnd(TestCaseEndArgs testCaseEndArgs)
@@ -36,17 +40,14 @@ public void TestSessionEnd(TestSessionEndArgs testSessionEndArgs)
3640

3741
try
3842
{
43+
_eqtTrace.Verbose($"Calling ModuleTrackerTemplate.UnloadModule for '{injectedInstrumentationClass.Assembly.FullName}'");
3944
var unloadModule = injectedInstrumentationClass.GetMethod(nameof(ModuleTrackerTemplate.UnloadModule), new[] { typeof(object), typeof(EventArgs) });
4045
unloadModule.Invoke(null, new[] { null, EventArgs.Empty });
46+
_eqtTrace.Verbose($"Called ModuleTrackerTemplate.UnloadModule for '{injectedInstrumentationClass.Assembly.FullName}'");
4147
}
4248
catch (Exception ex)
4349
{
44-
// Throw any exception if unload fails
45-
if (EqtTrace.IsErrorEnabled)
46-
{
47-
EqtTrace.Error("{0}: Failed to unload module with error: {1}", CoverletConstants.InProcDataCollectorName, ex);
48-
}
49-
50+
_eqtTrace.Error("{0}: Failed to unload module with error: {1}", CoverletConstants.InProcDataCollectorName, ex);
5051
string errorMessage = string.Format(Resources.FailedToUnloadModule, CoverletConstants.InProcDataCollectorName);
5152
throw new CoverletDataCollectorException(errorMessage, ex);
5253
}
@@ -57,7 +58,7 @@ public void TestSessionStart(TestSessionStartArgs testSessionStartArgs)
5758
{
5859
}
5960

60-
private static Type GetInstrumentationClass(Assembly assembly)
61+
private Type GetInstrumentationClass(Assembly assembly)
6162
{
6263
try
6364
{
@@ -74,11 +75,7 @@ private static Type GetInstrumentationClass(Assembly assembly)
7475
}
7576
catch (Exception ex)
7677
{
77-
// Avoid crashing if reflection fails.
78-
if (EqtTrace.IsWarningEnabled)
79-
{
80-
EqtTrace.Warning("{0}: Failed to get Instrumentation class with error: {1}", CoverletConstants.InProcDataCollectorName, ex);
81-
}
78+
_eqtTrace.Warning("{0}: Failed to get Instrumentation class with error: {1}", CoverletConstants.InProcDataCollectorName, ex);
8279
return null;
8380
}
8481
}

src/coverlet.collector/Utilities/TestPlatformEqtTrace.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,15 @@ public void Info(string format, params object[] args)
3939
{
4040
EqtTrace.Info($"[coverlet]{format}", args);
4141
}
42+
43+
/// <summary>
44+
/// Error logger
45+
/// </summary>
46+
/// <param name="format">Format</param>
47+
/// <param name="args">Args</param>
48+
public void Error(string format, params object[] args)
49+
{
50+
EqtTrace.Error($"[coverlet]{format}", args);
51+
}
4252
}
4353
}

0 commit comments

Comments
 (0)