Instrumenting .NET Applications

   

The .NET framework provides a number of base class libraries that help instrument .NET applications. They include the System.Management.Instrumentation, the System.Diagnostics, and the System.Management namespaces. Between these three namespaces you will find all the base class libraries that give WMI support.

Visual Studio .NET cannot automatically find the System.Management namespace. To use the System.Management namespace, you must add a reference to your project. To do this, right-click the project name in the Solution Explorer, select Add Reference, scroll down in the .NET list, select System.Management, and then click OK. From this point on you can use the System.Management namespace in your projects. Figure 19.1 shows how to add this reference to your project.

Figure 19.1. The Add Reference dialog box can be used to add the System.Management namespace.

graphics/19fig01.gif

The first example I have created to illustrate using WMI is called WMI Demos. The first demo page that I am going to talk about is one that enumerates all the services on the server. You can see the program executing in Figure 19.2. Among the services are those that query for disk information and some that query for performance statistics.

Figure 19.2. This application enumerates the services on a server and shows their status.

graphics/19fig02.gif

The code that does the enumeration can be seen in Listing 19.1. In this listing, you can see that the first thing that happens is that a ManagementObjectSearcher is created. The ManagementObjectSearcher constructor takes a single argument, which is a SelectQuery object. In this case, a new SelectQuery object is created that specifies the query will be performed on the WIN32_Service CIM object. This code then enumerates through the collection. Each time it walks through the collection, it obtains a ManagementObject class, with which it is able to determine the name and state of the service. Each side of the for each loop outputs the service name and the service state into an HTML table, which will become part of the HTML stream.

Listing 19.1 Using WMI to Enumerate Services and Their States
 public void EnumServices()  {      //Request the collection of services      ManagementObjectSearcher s =          new ManagementObjectSearcher( new SelectQuery( "Win32_Service" ) );      //Enumerate through the collection      foreach( ManagementObject service in s.Get() )      {          Response.Write( "<tr><td width=\"50%\">" + service["Name"] +              "</td><td width=\"50%\">" + service["State"] + "</td></tr>\r\n" );      }  } 

The second part of my WMI demo shows the free space from a logical disk named C. You can see this program running in Figure 19.3 as it shows the free space on disk C, which can be used to keep track of this important server attribute.

Figure 19.3. This WMI query shows the free space on logical disk C.

graphics/19fig03.gif

Listing 19.2 shows you the very simple code that was used to create the ManagementObject and to query for the free space on the logical disk.

Listing 19.2 Code to Show the Disk Free Space
 public void ShowFreeSpace()  {      ManagementObject o = new ManagementObject( "Win32_LogicalDisk='C:'" );      Response.Write( "The free space on logical disk 'c' is " +          o["FreeSpace"] );  } 
   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

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