.NET Development Foundation/Configuration

From Wikibooks, open books for an open world
Jump to: navigation, search

Configuration, diagnostic, management, and installation

Configuration, diagnostic, management, and installation[edit]

Exam objective: Embedding configuration, diagnostic, management, and installation features into a .NET Framework application


Configuration management[edit]

Configuration management is used here in a restricted sense. It refers to the adaptation of an application to a specific execution environment or user. This is usually done thru the use of configuration files that specify the run time parameters of the application. The typical example of such configuration information is the connection string used to locate and connect to the application database. The exam objectives are all related to the actual interaction of the application with its configuration files.

The management or the design of the configuration files themselves is a vast subject that is not touched by the exam objectives so we will not cover it in this study guide.

.NET framework installer[edit]

Event log[edit]

MSDN's definition is "Windows event logs allow your applications and components to record information about important events. You can use these records to audit access to your system, troubleshoot problems, and re-create usage patterns"

For a general discussion see MSDN

For specifics on the EventLog class and some cautions about its use see MSDN

Performance monitoring[edit]

Debugging and tracing[edit]

Management information and events[edit]

Windows Management Instrumentation - MSDN


To do:
Describe integration of WMI in .NET

Classes, Interfaces, and tools[edit]

Embed configuration management[edit]

Exam objective: Embed configuration management functionality into a .NET Framework application.

(Refer System.Configuration namespace)

Configuration class and ConfigurationManager class

Configuration class - MSDN
ConfigurationManager class - MSDN

ConfigurationSettings class, ConfigurationElement class, ConfigurationElementCollection class, and ConfigurationElementProperty class

ConfigurationSettings class - MSDN
ConfigurationElement class - MSDN
ConfigurationElementCollection class - MSDN
ConfigurationElementProperty class - MSDN

Implement IConfigurationSectionHandler interface - MSDN

ConfigurationSection class, ConfigurationSectionCollection class, ConfigurationSectionGroup class, and ConfigurationSectionGroupCollection class

ConfigurationSection class - MSDN
ConfigurationSectionCollection class - MSDN
ConfigurationSectionGroup class - MSDN
ConfigurationSectionGroupCollection - MSDN

Implement ISettingsProviderService interface - MSDN

Implement IApplicationSettingsProvider interface - MSDN

ConfigurationValidationBase class - MSDN

No direct result on MSDN - to be checked

Implement IConfigurationSystem interface - MSDN

Create custom installer and configure application[edit]

Exam objective: Create a custom Microsoft Windows Installer for .NET Framework components by using the System.Configuration.Install namespace, and configure the .NET Framework applications by using configuration files, environment variables, and the .NET Framework Configuration tool (Mscorcfg.msc).

For a "cookbook" on for the procedures discussed in this section see MSDN and the corresponding How-To section.

Installer class - MSDN

Configure which runtime version a .NET Framework application should use - MSDN

Configure where the runtime should search for an assembly - MSDN

Configure the location of an assembly and which version of the assembly to use - MSDN and MSDN

Direct the runtime to use the DEVPATH environment variable when searching for assemblies - MSDN

AssemblyInstaller class - MSDN

ComponentInstaller class - MSDN

Configure a .NET Framework application by using the .NET Framework Configuration tool (Mscorcfg.msc) - MSDN

ManagedInstaller class - MSDN

InstallContext class - MSDN

InstallerCollection class - MSDN

Implement IManagedInstaller interface - MSDN

InstallEventHandler delegate - MSDN

Configure concurrent garbage collection - MSDN

Register remote objects by using configuration files - MSDN

Manage an event log[edit]

Exam objective: Manage an event log by using the System.Diagnostics namespace

EventLog class - MSDN

EventSourceCreationData class - MSDN

Write to an event log[edit]


Read from an event log[edit]


Create a new event log[edit]

You create an EventLog by creating the first event source that writes to that log.

The two simplest way to do this are:

  • Use the EventLog.CreateEventSource method
  • Create an EventLog instance, specify a source and then write to the log. The actual creation takes place on execution of the first write.

Note that there is no "EventSource" class in the System.Diagnostics namespace even though an object representing the source is created in the registry.

C# EventLog creation Example

   using System;
   using System.Collections.Generic;
   using System.Text;
   using System.Diagnostics;
   namespace EventLogLab1
       class Program
           static void Main(string[] args)
                   EventLog log1 = new EventLog("EvtLab2Log");
                   log1.Source = "EvtLab2Source";
                   // Actual creation happens next
                   log1.WriteEntry("Example message", EventLogEntryType.Information,
                       123, 1);
               catch (Exception e)
               Console.WriteLine("Press ENTER to finish");

The recommended way, which does not seem to be covered on the Training Kit (so probably not on the exam) is to use the EventLogInstaller class during the installation of the application. For reference purposes see MSDN

Manage processes and monitor performance[edit]

Exam objective: Manage system processes and monitor the performance of a .NET Framework application by using the diagnostics functionality of the .NET Framework 2.0.

(Refer System.Diagnostics namespace)

Get a list of all running processes.

Process class - MSDN
For example code for GetCurrentProcess(), GetProcessesByName(), GetProcesses(), and GetProcessById() see MSDN

Retrieve information about the current process - MSDN

Get a list of all modules loaded by a process

The Process.Modules property returns a strongly-typed collection of ProcessModule objects that represent the Process' currently loaded modules.
For the Process.Modules property see MSDN
For the ProcessModule class see MSDN

PerformanceCounter class, PerformanceCounterCategory, and CounterCreationData class

PerformanceCounter class - MSDN
PerformanceCounterCategory - MSDN
CounterCreationData class - MSDN

Start a process both by using and by not using command-line arguments

Starting a Process Overview
Processes are started using the one of the overloaded Process.Start() methods. When passing sensitive data such as passwords to Process.Start(), use one of the two overloaded Start() methods that accept a SecureString as an argument type.
External links

StackTrace class - MSDN

StackFrame class - MSDN

Debug and Trace[edit]

Exam objective: Debug and trace a .NET Framework application by using the System.Diagnostics namespace.

Debug class and Debugger class

Debug class - MSDN
The four static writing methods of the Debug class - Write, WriteLine, WriteIf and WriteLineIf - let you write debug messages to inspect your program's flow and catch errors. Calls to these methods are ignored in the release version of your program (see 'The ConditionalAttribute attribute' below for details).
In Visual Studio, the default target for Debug's write methods is the Output window. You can use Debug's Listeners property to access the associated TraceListenerCollection. The following code shows how to use the Remove and Add methods of the collection to control where debug messages are sent to. If you add more than one listener of the same type, the associated target receives the text multiple times.
C# Code sample

Debug class example

   using System;
   using System.Diagnostics;
   using System.IO;
   using System.Reflection;
   class Program
       static void Main(string[] args)
           Debug.WriteLine("This is (by default) printed in the Output window.");
           //remove default listener
           //add a listener that can write to the Console window
           Debug.Listeners.Add(new ConsoleTraceListener());
           Debug.WriteLine("This is printed in the console window.");
           //add a default listener again
           Debug.Listeners.Add(new DefaultTraceListener());
           Debug.WriteLine("This is printed in both the Output and the Console window.");
           //remove all listeners
           //add a listener that writes to a file
           Debug.Listeners.Add(new TextWriterTraceListener(File.Create("C:\\test.txt")));
           Debug.WriteLine("This is only printed to the newly created file.");
           //here we need to flush the output buffer
           //keep console window open in debug mode
Debugger class - MSDN

Trace class - MSDN

CorrelationManager class - MSDN

TraceListener class - MSDN

TraceSource class - MSDN

TraceSwitch class - MSDN

XmlWriterTraceListener class - MSDN

DelimitedListTraceListener class - MSDN

EventlogTraceListener class - MSDN

Debugger attributes - MSDN

DebuggerBrowsableAttribute class - MSDN
DebuggerDisplayAttribute class - MSDN
DebuggerHiddenAttribute class - MSDN
DebuggerNonUserCodeAttribute class - MSDN
DebuggerStepperBoundaryAttribute class - MSDN
DebuggerStepThroughAttribute class - MSDN
DebuggerTypeProxyAttribute class - MSDN
DebuggerVisualizerAttribute class - MSDN

Embed management information[edit]

Exam objective: Embed management information and events into a .NET Framework application.

(Refer System.Management namespace - MSDN)

Retrieve a collection of Management objects by using the ManagementObjectSearcher class and its derived classes

The classes in the System.Management namespace let you use Windows Management Instrumentation (WMI) to manage computer systems. The most notable class in this namespace is ManagementObjectSearcher, that you can use to retrieve management objects based on a WMI query.
Information that can be retrieved this way ranges from computer case type (notebook, desktop...) over processor and hard disks details to information about running services and processes. Consult the WMI Reference on MSDN for an overview of all WMI classes and their properties.
In the example below a rough and dirty method is defined that prints all properties for all management information objects of a given WMI class. It is then used to print information about the current computer system.
C# Sample Code

WMI basic example

   class Program
       static void Main(string[] args)
           //Print all management info in the WMI class Win32_ComputerSystem
           //wait for user input to keep console window up in debug mode
       static void PrintManagementInfo(string WMIClassName)
           ManagementObjectSearcher mos;
           //Get all managementobjects of the specified class
           mos = new ManagementObjectSearcher("SELECT * FROM " + WMIClassName);
           foreach (ManagementObject MOCollection in mos.Get())
               foreach (PropertyData p in MOCollection.Properties)
                   //Some properties are arrays,
                   //in which case the code below only prints
                   //the type of the array.
                   //Add a check with IsArray() and a loop
                   //to display the individual array values.
                   Console.WriteLine("{0}: {1}", p.Name, p.Value);
ManagementObjectSearcher class - MSDN
Enumerate all disk drivers, network adapters, and processes on a computer
The following code uses one overload of the ManagementObjectSearcher constructor to list all physical and logical disk drives, network adapters and running processes on a system.
C# Sample Code

ManagementObjectSearcher example

   class Program
       static void Main(string[] args)
           Console.WriteLine("Physical disks: ");
           PrintManagementInfoProperty("Win32_DiskDrive", "Name");
           Console.WriteLine("Logical disks: ");
           PrintManagementInfoProperty("Win32_LogicalDisk", "Name");
           Console.WriteLine("Network adapters: ");
           PrintManagementInfoProperty("Win32_NetworkAdapter", "Name");
           Console.WriteLine("Processes: ");
           PrintManagementInfoProperty("Win32_Process", "Name");
           //wait for user input to keep console window up in debug mode
       static void PrintManagementInfoProperty(string WMIClassName, string WMIPropertyName)
           ManagementObjectSearcher mos;
           //Get the specified property for all objects of the specified class
           mos = new ManagementObjectSearcher("SELECT " + WMIPropertyName + " FROM " + WMIClassName);
           foreach (ManagementObject mo in mos.Get())
               PropertyData p = mo.Properties[WMIPropertyName]; 
               Console.WriteLine("{0}", p.Value);
Retrieve information about all network connections
Example to be provided
Retrieve information about all services that are paused
The following code uses a ManagementObjectSearcher object to retrieve all running services and then displays their names.
C# Sample Code

Enumarating services example

   class Program
       static void Main(string[] args)
           Console.WriteLine("Running services: ");
           //form the query, providing a WMI class name and a condition
           SelectQuery query = new SelectQuery("Win32_Service", "State='Running'");
           //find matching management objects
           ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
           foreach (ManagementObject mo in mos.Get())
           //wait for user input to keep console window up in debug mode

ManagementQuery class - MSDN

EventQuery class - MSDN

ObjectQuery class - MSDN

Subscribe to management events by using the ManagementEventWatcher class - MSDN

Previous / Next