WmiWebEventProvider 类

定义

实现将 ASP.NET 运行状况监视事件映射到 Windows Management Instrumentation (WMI) 事件的事件提供程序。

public ref class WmiWebEventProvider : System::Web::Management::WebEventProvider
public class WmiWebEventProvider : System.Web.Management.WebEventProvider
type WmiWebEventProvider = class
    inherit WebEventProvider
Public Class WmiWebEventProvider
Inherits WebEventProvider
继承
WmiWebEventProvider

示例

以下示例演示如何创建由 ASP.NET 运行状况监视作为 Web 应用程序运行状况事件而发出的 WMI 事件的使用者。

注意

WmiWebEventProvider默认情况下,已配置要监视的类和运行状况事件类型。 唯一需要做的就是为所有运行状况事件定义规则。 请记住,默认情况下不会将运行状况事件调度到 WmiWebEventProvider 提供程序。


using System;
using System.Management;

namespace SamplesAspNet
{
    // Capture WMI events associated with 
    // ASP.NET health monitoring types. 
    class SampleWmiWebEventListener
    {
        //Displays event related information.
        static void DisplayEventInformation(
            ManagementBaseObject ev)
        {

            // It contains the name of the WMI raised 
            // event. This is the name of the 
            // event class as defined in the 
            // Aspnet.mof file.
            string eventTypeName;

            // Get the name of the WMI raised event.
            eventTypeName = ev.ClassPath.ToString();

            // Process the raised event.
            switch (eventTypeName)
            {
                // Process the heartbeat event.  
                case "HeartBeatEvent":
                    Console.WriteLine("HeartBeat");
                    Console.WriteLine("\tProcess: {0}", 
                        ev["ProcessName"]);
                    Console.WriteLine("\tApp: {0}", 
                        ev["ApplicationUrl"]);
                    Console.WriteLine("\tWorkingSet: {0}", 
                        ev["WorkingSet"]);
                    Console.WriteLine("\tThreads: {0}", 
                        ev["ThreadCount"]);
                    Console.WriteLine("\tManagedHeap: {0}",
                        ev["ManagedHeapSize"]);
                    Console.WriteLine("\tAppDomainCount: {0}",
                        ev["AppDomainCount"]);
                    break;

                // Process the request error event. 
                case "RequestErrorEvent":
                    Console.WriteLine("Error");
                    Console.WriteLine("Url: {0}", 
                        ev["RequestUrl"]);
                    Console.WriteLine("Path: {0}", 
                        ev["RequestPath"]);
                    Console.WriteLine("Message: {0}", 
                        ev["EventMessage"]);
                    Console.WriteLine("Stack: {0}", 
                        ev["StackTrace"]);
                    Console.WriteLine("UserName: {0}", 
                        ev["UserName"]);
                    Console.WriteLine("ThreadID: {0}", 
                        ev["ThreadAccountName"]);
                    break;

                // Process the application lifetime event. 
                case "ApplicationLifetimeEvent":
                    Console.WriteLine("App Lifetime Event {0}", 
                        ev["EventMessage"]);
                   
                    break;

                // Handle events for which processing is not
                // provided.
                default:
                    Console.WriteLine("ASP.NET Event {0}",
                        ev["EventMessage"]);
                    break;
            }
        } // End DisplayEventInformation.

        // The main entry point for the application.
        static void Main(string[] args)
        {
            // Get the name of the computer on 
            // which this program runs.
            // Note. The monitored application must also run 
            // on this computer.
            string machine = Environment.MachineName;

            // Define the Common Information Model (CIM) path 
            // for WIM monitoring. 
            string path = String.Format("\\\\{0}\\root\\aspnet", 
                machine);

            // Create a managed object watcher as 
            // defined in System.Management.
            string query = "select * from BaseEvent";
            ManagementEventWatcher watcher =
                new ManagementEventWatcher(query);

            // Set the watcher options.
            TimeSpan timeInterval = new TimeSpan(0, 1, 30);
            watcher.Options = 
                new EventWatcherOptions(null,
                timeInterval, 1);

            // Set the scope of the WMI events to 
            // watch to be ASP.NET applications.
            watcher.Scope = 
                new ManagementScope(new ManagementPath(path));

            // Set the console background.
            Console.BackgroundColor = ConsoleColor.Blue;
            // Set foreground color.
            Console.ForegroundColor = ConsoleColor.Yellow;
            // Clear the console.
            Console.Clear();

            // Loop indefinitely to catch the events.
            Console.WriteLine(
                "Listener started. Enter CntlC to terminate");

            while (true)
            {
                try
                {
                    // Capture the WMI event related to 
                    // the Web event.
                    ManagementBaseObject ev = 
                        watcher.WaitForNextEvent();
                    // Display the Web event information.
                    DisplayEventInformation(ev);

                    // Prompt the user.
                    Console.Beep();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: {0}", e);
                    break;
                }
            }
        }
    }
}
Imports System.Management



' Capture WMI events associated with 
' ASP.NET health monitoring types. 

Class SampleWmiWebEventListener
    
    'Displays event related information.
    Shared Sub DisplayEventInformation(ByVal ev _
As ManagementBaseObject)

        ' It contains the name of the WMI raised 
        ' event. This is the name of the 
        ' event class as defined in the 
        ' Aspnet.mof file.
        Dim eventTypeName As String

        ' Get the name of the WMI raised event.
        eventTypeName = ev.ClassPath.ToString()

        ' Process the raised event.
        Select Case eventTypeName
            ' Process the heartbeat event.  
            Case "HeartBeatEvent"
                Console.WriteLine("HeartBeat")
                Console.WriteLine(vbTab + _
                "Process: {0}", ev("ProcessName"))
                Console.WriteLine(vbTab + "App: {0}", _
                ev("ApplicationUrl"))
                Console.WriteLine(vbTab + "WorkingSet: {0}", _
                ev("WorkingSet"))
                Console.WriteLine(vbTab + "Threads: {0}", _
                ev("ThreadCount"))
                Console.WriteLine(vbTab + "ManagedHeap: {0}", _
                ev("ManagedHeapSize"))
                Console.WriteLine(vbTab + "AppDomainCount: {0}", _
                ev("AppDomainCount"))

                ' Process the request error event. 
            Case "RequestErrorEvent"
                Console.WriteLine("Error")
                Console.WriteLine("Url: {0}", _
                ev("RequestUrl"))
                Console.WriteLine("Path: {0}", _
                ev("RequestPath"))
                Console.WriteLine("Message: {0}", _
                ev("EventMessage"))
                Console.WriteLine("Stack: {0}", _
                ev("StackTrace"))
                Console.WriteLine("UserName: {0}", _
                ev("UserName"))
                Console.WriteLine("ThreadID: {0}", _
                ev("ThreadAccountName"))

                ' Process the application lifetime event. 
            Case "ApplicationLifetimeEvent"
                Console.WriteLine("App Lifetime Event {0}", _
                ev("EventMessage"))


                ' Handle events for which processing is not
                ' provided.
            Case Else
                Console.WriteLine("ASP.NET Event {0}", _
                ev("EventMessage"))
        End Select

    End Sub

    ' End DisplayEventInformation.
    ' The main entry point for the application.
    Shared Sub Main(ByVal args() As String)
        ' Get the name of the computer on 
        ' which this program runs.
        ' Note. The monitored application must also run 
        ' on this computer.
        Dim machine As String = Environment.MachineName

        ' Define the Common Information Model (CIM) path 
        ' for WIM monitoring. 
        Dim path As String = _
        String.Format("\\{0}\root\aspnet", machine)

        ' Create a managed object watcher as 
        ' defined in System.Management.
        Dim query As String = "select * from BaseEvent"
        Dim watcher As New ManagementEventWatcher(query)

        ' Set the watcher options.
        Dim timeInterval As New TimeSpan(0, 1, 30)
        watcher.Options = _
        New EventWatcherOptions(Nothing, timeInterval, 1)

        ' Set the scope of the WMI events to 
        ' watch to be ASP.NET applications.
        watcher.Scope = _
        New ManagementScope(New ManagementPath(path))

        ' Set the console background.
        Console.BackgroundColor = ConsoleColor.Blue
        ' Set foreground color.
        Console.ForegroundColor = ConsoleColor.Yellow
        ' Clear the console.
        Console.Clear()

        ' Loop indefinitely to catch the events.
        Console.WriteLine( _
        "Listener started. Enter CntlC to terminate")


        While True
            Try
                ' Capture the WMI event related to 
                ' the Web event.
                Dim ev As ManagementBaseObject = _
                watcher.WaitForNextEvent()
                ' Display the Web event information.
                DisplayEventInformation(ev)

                ' Prompt the user.
                Console.Beep()

            Catch e As Exception
                Console.WriteLine("Error: {0}", e)
                Exit While
            End Try
        End While

    End Sub
End Class

以下示例是一个配置文件摘录,其中显示了一个 <healthMonitoring> 配置节,使 ASP.NET 能够使用 WmiWebEventProvider 提供程序来处理所有运行状况监视事件。

<healthMonitoring>  
  <rules>  
    <add   
      name="Using Wmi"  
      eventName="All Events"   
      provider="WmiWebEventProvider"   
      profile="Critical"/>  
  </rules>  
</healthMonitoring>  

注解

ASP.NET 运行状况监视允许生产和运营人员管理已部署的 Web 应用程序。 命名空间 System.Web.Management 包含负责打包应用程序运行状况状态数据的运行状况事件类型和负责处理此数据的提供程序类型。 它还包含有助于管理运行状况事件的支持类型。

ASP.NET 使用此类将运行状况监视事件映射到 WMI 事件。 若要将 ASP.NET 运行状况监视事件传送到 WMI 子系统,必须通过在配置文件的 节中添加相应的设置<healthMonitoring>来配置 WmiWebEventProvider 类。

Aspnet.mof 文件中包含的信息描述了 ASP.NET 运行状况监视事件路由到 WmiWebEventProvider 类并映射到 WMI 事件时引发的 WMI 事件的参数。 Aspnet.mof 文件存储在 .NET Framework 生成目录中,例如 %windir%\Microsoft.NET\Framework\BuildNumber。 有关将运行状况监视事件报告为 WMI 事件的详细信息,请参阅 使用 WMI 传递 ASP.NET 运行状况监视事件

注意

在大多数情况下,你将能够使用实现的 ASP.NET 运行状况监视类型,并通过在 <healthMonitoring> 配置部分中指定值来控制运行状况监视系统。 还可以从运行状况监视类型派生,以创建自己的自定义事件和提供程序。 有关创建自定义提供程序的示例,请参阅 如何:实现运行状况监视自定义提供程序示例

构造函数

WmiWebEventProvider()

初始化 WmiWebEventProvider 类的新实例。

属性

Description

获取一条简短的易懂描述,它适合在管理工具或其他用户界面 (UI) 中显示。

(继承自 ProviderBase)
Name

获得一个友好名称,用于在配置过程中引用提供程序。

(继承自 ProviderBase)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Flush()

从提供程序的缓冲区中移除所有事件。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
Initialize(String, NameValueCollection)

设置此对象的初始值。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ProcessEvent(WebBaseEvent)

处理传递给提供程序的事件。

Shutdown()

执行与关闭提供程序相关联的任务。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅