Thursday, September 6, 2007

Getting line numbers in exception stack trace in a Windows Service

The Exception.ToString() method contains call stack information when the exception was thrown. However if the PDB files from the assemblies involved are available, this information also contains file names and line numbers at each level of the stack trace. Needless to say that this information can be very beneficial to the developer.

The CLR will look for the PDB files in the runtime working directory, which for most applications is the installation directory itself. However for windows services the working directory is the windows systems32 directory (For eg: C:\Windows\System32). So exceptions thrown in assemblies that are hosted in a windows service, the call stack rarely contains the file names and line numbers even when the right PDB files are available.

The working directory of a windows service can be changed using the following line:

   1: Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

Once the working directory has been changed to the base directory, the CLR should pick up the right PDB files and the stack trace should contain line numbers.


Technorati Tags: , , ,

No comments:

Post a Comment

Subscribe to my feed in your favorite feed reader