As part of a development process we decided that it would be useful to allow for logging to be produced while we are testing our new class library. The log4net config is determined by the consuming application and the libary will make use of that.
We realised that the test project is a consumer of the dll and so we wanted to be able to configure the log4net environment in the test project so that it would log to the output folder of the test results.
This proved quite problematic as at first it was unable to find the log4net.config file where we determine the loggers etc for log4net. When MSTest runs tests it put its test results along side the dlls in a separate folder for each test run. We wanted to make the log4net.config file be in each of these folders. To do this we had to edit the .testrunconfig file that gets created when you run MSTests. In the deployment section we added the file "\testlog4net.config". This now placed the config file in this test directory allowing for our log4net config code to load it properly.
The issue now was that the log file was being saved to an unknow location. We were able to hard code the filepath to c:\{ApplicationName}.log but this is not the desired effect. We could hard code it so that it built to the Debug folder of the test project but its not a relative path so would cause problems in the future should the source change location ona developers machine. The solution is a work around by making the log dir be dynamic.
We also found that if you just supply the log filename then it saves it to C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE. The log4net .config file has an element that determine the destination log file.
We realised that if we altered the XML before configring log4net then we could determine the exact location of the log file.
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
// Only configure log4net once per process
if (!LogManager.GetRepository().Configured)
{
string log4netFilename = "log4net.config";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(log4netFilename);
XmlNode node = xmlDoc.SelectSingleNode("log4net/appender/param[@name=\"File\"]");
node.Attributes["value"].Value = Path.Combine(Directory.GetCurrentDirectory(),node.Attributes["value"].Value);
XmlElement element = xmlDoc.DocumentElement;
log4net.Config.XmlConfigurator.Configure(element);
if (log.IsDebugEnabled)
{
log.DebugFormat("Loaded log4net config in {0}.", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly.FullName);
}
}
else
{
if (log.IsDebugEnabled)
{
log.DebugFormat("log4net already configured for {0}.", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly.FullName);
}
}
log.Info("Application Started");
}
This will now save logs to the Out directory in the TestResults.
We realised that the test project is a consumer of the dll and so we wanted to be able to configure the log4net environment in the test project so that it would log to the output folder of the test results.
This proved quite problematic as at first it was unable to find the log4net.config file where we determine the loggers etc for log4net. When MSTest runs tests it put its test results along side the dlls in a separate folder for each test run. We wanted to make the log4net.config file be in each of these folders. To do this we had to edit the .testrunconfig file that gets created when you run MSTests. In the deployment section we added the file "\testlog4net.config". This now placed the config file in this test directory allowing for our log4net config code to load it properly.
The issue now was that the log file was being saved to an unknow location. We were able to hard code the filepath to c:\{ApplicationName}.log but this is not the desired effect. We could hard code it so that it built to the Debug folder of the test project but its not a relative path so would cause problems in the future should the source change location ona developers machine. The solution is a work around by making the log dir be dynamic.
We also found that if you just supply the log filename then it saves it to C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE. The log4net .config file has an element that determine the destination log file.
We realised that if we altered the XML before configring log4net then we could determine the exact location of the log file.
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
// Only configure log4net once per process
if (!LogManager.GetRepository().Configured)
{
string log4netFilename = "log4net.config";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(log4netFilename);
XmlNode node = xmlDoc.SelectSingleNode("log4net/appender/param[@name=\"File\"]");
node.Attributes["value"].Value = Path.Combine(Directory.GetCurrentDirectory(),node.Attributes["value"].Value);
XmlElement element = xmlDoc.DocumentElement;
log4net.Config.XmlConfigurator.Configure(element);
if (log.IsDebugEnabled)
{
log.DebugFormat("Loaded log4net config in {0}.", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly.FullName);
}
}
else
{
if (log.IsDebugEnabled)
{
log.DebugFormat("log4net already configured for {0}.", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly.FullName);
}
}
log.Info("Application Started");
}
This will now save logs to the Out directory in the TestResults.
