共用方式為


引發自訂 ASP.NET 健康監視事件範例

更新:2007 年 11 月

這個主題中的程式碼範例示範如何實作能夠引發自訂 ASP.NET 健康監視事件的 IHttpModule 模組。如需如何使用這個程式碼範例的詳細資訊,請參閱 HOW TO:實作和引發自訂 ASP.NET 健康監視事件

範例

下列程式碼範例示範如何衍生自 WebRequestEvent 類別以建立自訂 ASP.NET 健康監視事件。為了提供指示,程式碼會包含在引發自訂事件的 HTTP 模組實作中。此外,您也可以從其他 ASP.NET 網頁或組件引發自訂事件。與內建 ASP.NET 健康監視事件不同之處在於,自訂事件必須明確引發。

using System;
using System.Web;
using System.Web.Management;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Samples.AspNet.Management
{

    public class CustomWebEvents : Page, IHttpModule
    {

        public override void Dispose()
        {
        }

        // Add event handlers to the HttpApplication.
        public new void Init(HttpApplication httpApp)
        {
            httpApp.BeginRequest +=
                new EventHandler(OnBeginRequest);

            httpApp.EndRequest +=
                new EventHandler(OnEndRequest);

        }

        // Issues a custom begin request event.
        public void OnBeginRequest(Object sender, EventArgs e)
        {

            HttpApplication httpApp = sender as HttpApplication;

            try
            {
                // Make sure to be outside the forbidden range.
                System.Int32 myCode = WebEventCodes.WebExtendedBase + 30;
                SampleWebRequestEvent swre =
                  new SampleWebRequestEvent(
                  "SampleWebRequestEvent Start", this, myCode);
                // Raise the event.
                swre.Raise();
            }
            catch (Exception ex)
            {
                httpApp.Context.Response.Output.WriteLine(
                    ex.ToString());
            }

        }

        // Issues a custom end request event.
        public void OnEndRequest(Object sender, EventArgs e)
        {
            HttpApplication httpApp = sender as HttpApplication;

            try
            {
                // Make sure to be outside the forbidden range.
                System.Int32 myCode = WebEventCodes.WebExtendedBase + 40;
                SampleWebRequestEvent swre =
                  new SampleWebRequestEvent(
                  "SampleWebRequestEvent End", this, myCode);
                // Raise the event.
                swre.Raise();
            }
            catch (Exception ex)
            {
                httpApp.Context.Response.Output.WriteLine(
                    ex.ToString());
            }

        }
    }
    public class SampleWebRequestEvent : System.Web.Management.WebRequestEvent
    {
        private string customCreatedMsg;
        private string customRaisedMsg;

        // Invoked in case of events identified only by their event code.
        public SampleWebRequestEvent(
            string msg,
            object eventSource,
            int eventCode)
            :
            base(msg, eventSource, eventCode)
        {
            // Perform custom initialization.
            customCreatedMsg = string.Format("Event created at: {0}",
                EventTime.ToString());
        }

        // Invoked in case of events identified by their event code and 
        // related event detailed code.
        public SampleWebRequestEvent(
            string msg,
            object eventSource,
            int eventCode,
            int eventDetailCode)
            :
            base(msg, eventSource, eventCode, eventDetailCode)
        {
            // Perform custom initialization.
            customCreatedMsg = string.Format("Event created at: {0}",
                EventTime.ToString());

        }

        // Raises the SampleWebRequestEvent.
        public override void Raise()
        {
            // Perform custom processing.
            customRaisedMsg = string.Format("Event raised at: {0}",
                EventTime.ToString());

            // Raise the event.
            base.Raise();
        }

        //Formats Web request event information.
        public override void FormatCustomEventDetails(
            WebEventFormatter formatter)
        {
            // Add custom data.
            formatter.AppendLine("");

            formatter.IndentationLevel += 1;
            formatter.AppendLine("* Custom Request Information Start *");

            // Display custom event timing.
            formatter.AppendLine(customCreatedMsg);
            formatter.AppendLine(customRaisedMsg);

            formatter.AppendLine("* Custom Request Information End *");

            formatter.IndentationLevel -= 1;
        }
    }
}

請參閱

工作

HOW TO:實作和引發自訂 ASP.NET 健康監視事件

HOW TO:實作健康監視自訂提供者範例

概念

ASP.NET 健康監視事件概觀