Wednesday, 26 November 2008

Enable logging while running MSTests

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.

Wednesday, 17 September 2008

Don't drag a folder in Microsoft Visual SourceSafe

One of our developers just managed to drag a folder from one location to another in the Visual SourceSafe Client application.

We were at the same time getting the latest version of the source code on our build machine and I inadvertantly got this change on the build machine.

We have now had to stop the build process until the source is restored to it's former glory.

Microsoft Windows SQL 2005 Hotfix

I had an issue installing a Hotfix for SQL Server 2005. My developer machine has SQL Server 2005 and SQL Express 2005 installed. I was initially trying to install the Hotfix file through Windows Automatic Updates but it kept failing. I went to the msdn site and downloaded the separate executable and fired the install off.



Everything seemed to be ok... The process fired back the response that I needed to stop the SQL services in order to upgrade them so I did this. A few seconds later it failed again.

Time: 09/16/2008 17:33:26.984
KB Number: KB948109
Machine: STEELRABBIT
OS Version: Microsoft Windows XP Professional Service Pack 3 (Build 2600)
Package Language: 1033 (ENU)
Package Platform: x86
Package SP Level: 2
Package Version: 3068
Command-line parameters specified:
Cluster Installation: No

**********************************************************************************
Prerequisites Check & Status
SQLSupport: Passed

**********************************************************************************
Products Detected Language Level Patch Level Platform Edition
SQL Server Database Services 2005 (MSSQLSERVER) ENU SP2 2005.090.3068.00 x86 DEVELOPER
SQL Server Database Services 2005 (SQLEXPRESS) ENU SP2 2005.090.3042.00 x86 EXPRESS
SQL Server Tools and Workstation Components 2005 ENU SP2 9.2.3068 x86 DEVELOPER

**********************************************************************************
Products Disqualified & Reason
Product Reason

**********************************************************************************
Processes Locking Files
Process Name Feature Type User Name PID

**********************************************************************************
Product Installation Status
Product : SQL Server Database Services 2005 (MSSQLSERVER)
Product Version (Previous): 3068
Product Version (Final) :
Status : Failure
Log File : C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\SQL9_Hotfix_KB948109_sqlrun_sql.msp.log
Error Number : 1635
Error Description : Unable to install Windows Installer MSP file
----------------------------------------------------------------------------------
Product : SQL Server Database Services 2005 (SQLEXPRESS)
Product Version (Previous): 3042
Product Version (Final) :
Status : Failure
Log File : C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\SQL9_Hotfix_KB948109_sqlrun_sql.msp.log
SQL Express Features :
Error Number : 1635
Error Description : Unable to install Windows Installer MSP file
----------------------------------------------------------------------------------
Product : SQL Server Tools and Workstation Components 2005
Product Version (Previous): 3068
Product Version (Final) :
Status : Failure
Log File : C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\SQLTools9_Hotfix_KB948109_sqlrun_tools.msp.log
Error Number : 1635
Error Description : Unable to install Windows Installer MSP file
----------------------------------------------------------------------------------

**********************************************************************************
Summary
One or more products failed to install, see above for details
Exit Code Returned: 1635


I saught the help of our administrator to see if we could work out the issue. We noticed that the error log was failing to read the MSP file from my x:\ drive. A virtual drive linked to a folder on my c:\ drive. We presumed that that Microsoft in their infinite wisdom decide that extracting their patch files to c:\ drive is a hazard. Or that it trys to extract them to the drive with the most amount of free space. As the x:\ and c:\ drives have the same amount of space the installers decide to use the x:\ drive as I guess Microsoft presume the c:\ drive will only get smaller as programs are installed and configured.

The fix for this was to delv into the msdn site and find an article that gives the command line options for hotfix files.

Solution:
  1. Open a command line window where the downloaded hotfix file is located.
  2. Execute the hotfix file with the /x option. (eg. SQLServer2005-KB948109-x86-ENU.exe /x)
  3. Choose a location to extract the install files to. (eg. c:\temp)
  4. Run the Hotfix.exe from the location the files were extracted.
I'll have a search for the real reason the files were extracted to my x:\ drive at some point. As for why the installer couldn't read from this location is bewildering.