Recipe8.18.Looking at Your Runtime Environment and Seeing What You Can Do About It


Recipe 8.18. Looking at Your Runtime Environment and Seeing What You Can Do About It

Problem

You want a way to easily gather information about the environment your program is running under to assist in troubleshooting customer issues.

Solution

Write a utility function that can gather this information for the process using the System.Environment class members. To view and manipulate the environment variables, use the GetEnvironmentVariables, GetEnvironmentVariable, and SetEnvironmentVariable methods:

 Console.WriteLine("Current environment settings:"); foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) {     Console.WriteLine("\t\tEnvironment Variable {0}: Value {1}", de.Key, de.Value); } Console.WriteLine("\tSetting environment variable MYENVVAR to {0}", "FOO"); Environment.SetEnvironmentVariable("MYENVVAR", "FOO"); Console.WriteLine("\tGetting Environment Variable MYENVVAR: Value {0}", Environment. GetEnvironmentVariable("MYENVVAR")); 

The command-line parameters, current working directory, and system directory are available from the CommandLine, CurrentDirectory, and SystemDirectory properties, respectively:

 Console.WriteLine("\tCommand Line: {0}",Environment.CommandLine); Console.WriteLine("\tCurrent Directory: {0}",Environment.CurrentDirectory); Console.WriteLine("\tSystem Directory: {0}",Environment.SystemDirectory); 

For machine-specific information, the machine name (MachineName), operating system version (OSVersion), and number of processors (ProcessorCount) are easily accessed:

 Console.WriteLine("\tMachine NetBIOS Name: {0}",Environment.MachineName); Console.WriteLine("\tOS Version Information: {0}",Environment.OSVersion. VersionString); Console.WriteLine("\tNumber of processors: {0}",Environment.ProcessorCount); 

To see the current call stack, use the StackTrace property:

 Console.WriteLine("\tCurrent Stack Trace: {0}",Environment.StackTrace); 

For information about the current user and thread, the UserDomainName, UserInteractive, and UserName properties do the job:

 Console.WriteLine("\tCurrent User Domain Name: {0}",Environment.UserDomainName); Console.WriteLine("\tIs this an interactive user process?: {0}",Environment. UserInteractive); Thread.CurrentThread.Name = "Main thread"; Console.WriteLine("\tUser who started Thread ({0}): {1}",Thread.CurrentThread.Name,     Environment.UserName); 

Finally, the CLR version and amount of physical memory being used by the process are available in the Version and WorkingSet properties:

 Console.WriteLine("\tCLR Version this code is running in: {0}",Environment.Version); Console.WriteLine("\tAmount of physical memory used by this working set: {0}", Environment.WorkingSet); 

Discussion

Adding code to your programs for troubleshooting potential customer issues may seem like a hassle when you are coding, but it can be a lifesaver when you get that call on Friday morning that threatens to ruin your weekend. Having additional information about the environment the customer is running your code in can lead to the discovery of bad settings, interaction conflicts with other third-party software, or the uncovering of the source of the issue itself. System.Environment has a lot of valuable information that is worth making available for your support folks; they will thank you for it.

See Also

See the "System.Environment Class" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net