Walkthrough: Listening for WMI Events in ASP.NET Health Monitoring

This walkthrough shows how to listen for Windows Management Instrumentation (WMI) events that have been mapped to ASP.NET health monitoring events. Tasks illustrated in this walkthrough include:

  • Building a console application that listens for health monitoring events.

  • Configuring a Web application to be monitored.

  • Testing the console application.

For more information about using WMI to deliver health monitoring events, see Using WMI to Deliver ASP.NET Health Monitoring Events.

In order to complete this walkthrough, you will need:

  • Access to a server with Microsoft Internet Information Services (IIS) and the .NET Framework version 2.0 installed.

  • Sufficient permissions to be able to create an ASP.NET Web site on the server.

  • Permission to run the application with administrative privileges to access the Web events information.

The following list describes the main steps that occur during ASP.NET health monitoring if the delivery of event data is configured to be processed through WMI:

  1. ASP.NET raises a health event, according to the configuration settings.

  2. ASP.NET health monitoring, based on the configuration settings, dispatches the event to the WmiWebEventProvider provider.

  3. The WmiWebEventProvider provider, based on the information contained in the WMI Managed Object Format (MOF) file for ASP.NET (Aspnet.mof), passes the event information to the WMI system through an unmanaged call.

  4. Finally, WMI passes the relevant data to the custom application. This application uses the System.Management types to interoperate between the ASP.NET managed and the WMI unmanaged APIs.

The following illustration depicts this process.

ASP.NET health monitoring process using WMI
WMI Listener

Standard steps for using WMI to listen for ASP.NET health monitoring events are as follows:

  1. Create and build a custom listener application. In this walkthrough, your listener application is a console application.

  2. Configure your Web application to allow the custom provider to process health events by enabling health monitoring and specifying a provider. In this walkthrough, your provider is the default WMI provider.

  3. Activate the listener on the server where your Web application runs. Because your listener is a console application, this means running your application from the command line.

  4. Access any page of the monitored Web site in a Web browser.

  5. Verify that the health events issued by the Web site are captured by the listener.

A WMI event listener processes ASP.NET health events after they have been forwarded to WMI.

The code example in the following procedure is for a WMI event listener console application that accesses the WMI objects that are associated with ASP.NET health events. The following list describes the main steps performed by this application:

  • Get the name of the computer on which the listener and the Web application run.

  • Set the WMI path for the ASP.NET entities.

  • Create a managed object watcher as defined in the System.Management namespace.

  • Set the scope of events to watch to be equal to the previous WMI path.

  • Loop indefinitely to catch the WMI events.

To create a WMI event listener for ASP.NET health events

  1. Create a source file for a console application, and paste in the following code. This code listens for WMI events associated with ASP.NET health monitoring events on the local computer, which is the same computer where the monitored Web application runs.

    No code example is currently available or this language may not be supported.
  2. Build the console application. You might need to include a reference for the System.Management namespace.

The standard WmiWebEventProvider provider contains the logic to generate the WMI events associated with the ASP.NET health events.

To configure an ASP.NET application to use WMI to listen for ASP.NET health monitoring events

  1. Create or open a Web.config file at the root of your application.

    A basic Web.config file looks like the following code example.

    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  2. Enable health monitoring by adding the following lines of code inside the system.web section.

            <healthMonitoring enabled="true">
  3. Configure the application to use the WmiWebEventProvider provider to deliver health monitoring data by adding the following lines of code inside the healthMonitoring section. Note that the WmiWebEventProvider provider is already configured. To allow the processing of Web events, you just need to define a rule as follows.

                     name="Wmi Listener"
                     eventName="All Events" 

    The eventName attribute of the added rule allows for all the Web events to be dispatched to the WmiWebEventProvider provider.

After you have created and built your listener application and configured a Web site to enable health monitoring through WMI events, you can verify that health events are captured by running your listener application while you request a page from your Web site.

To test your listener console application

  1. Create an ASP.NET page in the same Web application that you configured to enable health monitoring.

  2. From the command line, run your listener application.

    The command window changes color to a blue background with the following yellow text displayed:

    Listener started. Enter CntlC to terminate
  3. In a Web browser, request the page from your Web application.

    Before the page finishes rendering, the command window that is running your listener application displays the following text:

    ASP.NET Event URL authorization succeeded for the request.
    ASP.NET Event File authorization succeeded for the request.

    This verifies that your listener has caught the URL authorization event and the file authorization event from ASP.NET.

The WMI listener shown is a simple console application, but it illustrates the fundamental steps to build a WMI listener and configure an application to dispatch health events. From this start, you can begin to explore additional ways to monitor your Web applications. Suggestions for more exploration include:

  • Investigate the use of Windows applications to collect WMI events.

  • Explore the use of more complex processing capabilities based on the type of event.

  • Learn about WMI and how the .NET Framework integrates its functionality. For more information, see System.Management, System.Management.Instrumentation.

  • Search the MSDN library for Windows Management Instrumentation (WMI) and Managed Object Format (MOF).

Community Additions