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 健康情況監視所發出 WMI 事件的取用者,因為 Web 應用程式健康情況事件。

注意

預設已設定要 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 子系統,您必須 WmiWebEventProvider 在組態檔的 區段中新增適當的設定 <healthMonitoring> 來設定 類別。

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)

適用於

另請參閱