1
votes

I'm managing the build of a C# project using NuGet to obtain Common.Logging.Log4Net. Unit tests all pass with no errors when run from Visual Studio 2010, but when I run from the command line using MSTest (and therefore when TeamCity does the same), I get failures when the unit test hist a class that does this:

private static readonly ILog Log = LogManager.GetCurrentClassLogger();

and the error is:

Test method MyProject.Tests.Unit.BusinessDateBuilderTest.CreateElementalTest threw exception: System.TypeInitializationException: The type initializer for 'MyProject.Common.Library' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MyProject.Common.Initialization.Impl.InitializationLoaderImpl' threw an exception. ---> Common.Logging.ConfigurationException: Failed obtaining configuration for Common.Logging from configuration section 'common/logging'. ---> System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for common/logging: Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net' (C:\Subversion\Project\MyProject.Tests.Unit\bin\Debug\TestResults\rac_DWM300619 2012-10- 30 11_58_34\Out\MyProject.Tests.Unit.dll.config line 168) ---> Common.Logging.ConfigurationException: Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net' ---> System.IO.FileNotFoundException: Could not load file or assembly 'Common.Logging.Log4net' or one of its dependencies. The system cannot find the file specified.

I'm using the following NuGet dependencies:

Common.Logging.2.1.1 (DLL version is 2.1.1.0) Common.Logging.Log4Net.2.0.1 (DLL version is 2.0.0.0) log4net.1.2.10 (DLL version is 1.2.10)

I have noticed that Common.Logging.Log4net.dll is not copied to the testresults output folder - is this the problem? If so, how do I resolve this?

1
Make sure the Common.Logging.Log4net assembly is copied to the directory where the test runs.Steven
Yes, but how? Common.Logging.dll is copied, but Common.Logging.Log4net.dll is not.RCross
Add a reference to the assembly from within your application.Steven

1 Answers

2
votes

MSTest some times copies assemblies to a new folder and runs the tests from there. The problem is that some versions (all versions?) use some kind of static analysis of what assemblies to copy. And since the log4net connection from common logging is very dynamic (the purpose of using common logging) the assembly is never copied hence your error.
If you want to fix this you need to create a testsettings file for MSTest (from inside of Visual Studio). And then add deploymentItems (your missing assemblies) to it. Then you need to point your command line to it. In teamcity you do this in "MSTest run configuration file" when configuring the MsTest runner.