다음을 통해 공유


사용자 지정 ASP.NET 상태 모니터링 이벤트 예제 발생

업데이트: 2007년 11월

이 항목의 예제에서는 사용자 지정 ASP.NET 상태 모니터링 이벤트를 발생시킬 수 있는 IHttpModule 모듈을 구현하는 방법을 보여 줍니다. 이 예제를 사용하는 방법에 대한 자세한 내용은 방법: 사용자 지정 ASP.NET 상태 모니터링 이벤트 구현 및 발생을 참조하십시오.

예제

다음 코드 예제에서는 사용자 지정 ASP.NET 상태 모니터링 이벤트를 만들기 위해 WebRequestEvent 클래스에서 파생시키는 방법을 보여 줍니다. 내용 설명을 위해 코드는 사용자 지정 이벤트를 발생시키는 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;
        }
    }
}

참고 항목

작업

방법: 사용자 지정 ASP.NET 상태 모니터링 이벤트 구현 및 발생

방법: 상태 모니터링 사용자 지정 공급자 예제 구현

개념

ASP.NET 상태 모니터링 개요