Wednesday, May 09, 2007

I recently wrote a simple class for generating diagnostic information that should prove useful in tracking down errors when the system goes live. It details the operating system and .NET version installed on the current machine, the version and location of the entry assembly and the currently logged in OS user:

ENTRY ASSEMBLY
--------------

File                           : C:\Projects\SystemDiagnosticDemo\SystemDiagnosticDemo\bin\Debug\SystemDiagnosticDemo.exe
FileVersion                    : 1.0.0.123
Product                        : SystemDiagnosticDemo
ProductVersion                 : 1.0.0.123

SERVER
------

Machine Name                   : STUART-LAPTOP
Operating System               : Microsoft Windows NT 5.1.2600 Service Pack 2
Processor Count                : 1
.NET Version                   : 2.0.50727.42
OS User                        : STUART-LAPTOP\Stuart

 

As part of the automated build, the nant script creates an AssemblyInfo.cs file containing the Cruisecontrol build number. Cruisecontrol is also setup to tag the source code in Subversion with the same build number to give us full traceability. This means we can determine the exact source code that went into a particular dll and bring up the build log containing the unit test results.

The assembly metadata is retrieved using System.Diagnostics.FileVersionInfo.GetVersionInfo() method on the entry assembly file's location:

Assembly assembly = Assembly.GetEntryAssembly();
string assemblyFileName = assembly.Location;
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(assemblyFileName);

string fileName = versionInfo.FileName;
string fileVersion = versionInfo.FileVersion;
string product = versionInfo.ProductName;
string productVersion = versionInfo.ProductVersion;

and the environment information comes from the static properties on the System.Environment class:

string machineName = Environment.MachineName;
string operatingSystem = Environment.OSVersion.ToString();
int processorCount = Environment.ProcessorCount;
string dotNetVersion = Environment.Version.ToString();
string osUser = String.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName);

The demo source code is available here:

SystemDiagnosticDemo.zip (11.09 KB)
Wednesday, May 09, 2007 8:34:31 PM (GMT Daylight Time, UTC+01:00)