Log4net cannot find configuration file when starting from Visual Studio / Microsoft Test Framework

We are writing unit tests for our business layer running under .NET 4.0. The business layer is a simple C # class library that typically runs on SOAP and REST web services. Our application uses log4net in a separate assembly for registration. The C # code in the protocol assembly contains an assembly information directive that tells log4net the name of the configuration file, a la -

[build: log4net.Config.XmlConfigurator (ConfigFile = "AcmeLogging.config", Watch = true)]

Initializing log4net through the shell works fine in web services. When we initialize it using our unit test assembly, it does not appear in the configuration file. The configuratino file is configured using the properties that need to be copied to the execution directory, and we see it in the bin \ debug directory. A quick console test program using a logging node that works from the same folder works fine. It is curious that behavior problems are intermittent and appear on different machines of developers at different times and cannot be cured in any deterministic way.

When executing the wrapper assembly code, the call to log4netLogManager.GetLogger () appears correctly, but the list of add-ons returned by log.Logger.Repository.GetAppenders () is empty. Since this incorrect behavior is the same regardless of whether the file is in the Bin \ Debug folder or not, we believe that it does not see the file.

Any hints regarding what we are missing in starting log4net in the Microsoft Test Framework would be greatly appreciated.

+7
source share
1 answer

Unit tests are unique in that if you need configuration files in unit tests, you need to include them as deployment elements. Here is an example of how I do this in my test class:

[TestClass] [DeploymentItem("hibernate.cfg.xml")] public class AsyncForwardingAppenderTest { } 

In addition to the Deployment attribute, you need to enable deployment in the test settings. To do this, go to Test-> Edit Test Settings->. Then click on the Deployment on the right. Check the Enable Deployment box.

After this and starting the test, your configuration file should be located in the test results folder. TestResults \ username_machine date stamp \ Out . If your configuration file is not in this folder, it will not work. This is what the DeploymentItem attribute does. It attaches a file in this Out folder.

If you do not want to include the DeploymentItem attribute in each test class, then I had to create a base test class so that all tests using log4net inherit and mark it with the DeploymentItem attribute.

+8
source

All Articles