WebBaseEvent 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義 ASP.NET 健康監視事件的基底類別。
public ref class WebBaseEvent
public class WebBaseEvent
type WebBaseEvent = class
Public Class WebBaseEvent
- 繼承
-
WebBaseEvent
- 衍生
範例
下列範例示範如何衍生自 類別, WebBaseEvent 以建立自訂健全狀況監視事件。
using System;
using System.Text;
using System.Web;
using System.Web.Management;
namespace SamplesAspNet
{
// Implements a custom WebBaseEvent class.
public class SampleWebBaseEvent :
System.Web.Management.WebBaseEvent, IWebEventCustomEvaluator
{
private string customCreatedMsg, customRaisedMsg;
// Store firing record info.
private static string firingRecordInfo;
// Implements the IWebEventCustomEvaluator.CanFire
// method. It is called by the ASP.NET if this custom
// type is configured in the profile
// element of the healthMonitoring section.
public bool CanFire(
System.Web.Management.WebBaseEvent e,
RuleFiringRecord rule)
{
bool fireEvent;
string lastFired = rule.LastFired.ToString();
string timesRaised = rule.TimesRaised.ToString();
// Fire every other event raised.
fireEvent =
(rule.TimesRaised % 2 == 0) ? true : false;
if (fireEvent)
{
firingRecordInfo =
string.Format("Event last fired: {0}",
lastFired) +
string.Format(". Times raised: {0}",
timesRaised);
}
else
firingRecordInfo =
string.Format(
"Event not fired. Times raised: {0}",
timesRaised);
return fireEvent;
}
// Invoked in case of events identified only by
// their event code.
public SampleWebBaseEvent(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 SampleWebBaseEvent(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 SampleWebBaseEvent.
public override void Raise()
{
// Perform custom processing.
customRaisedMsg =
string.Format("Event raised at: {0}",
EventTime.ToString());
// Raise the event.
base.Raise();
}
// Raises the SampleWebBaseEvent.
public void CustomRaise(
System.Web.Management.WebBaseEvent evnt)
{
// Raise the event.
Raise(evnt);
}
// Gets the event code.
public int GetEventCode(bool detail)
{
int eCode;
if (!detail)
// Get the event code.
eCode = EventCode;
else
// Get the detail event code.
eCode = EventDetailCode;
return eCode;
}
// Gets the event sequence.
public long GetEventSequence()
{
// Get the event sequence.
long eventSequence = EventSequence;
return eventSequence;
}
// Gets the event source.
public Object GetEventSource()
{
// Get the event source.
Object source = this.EventSource;
return source;
}
// Gets the event time.
public DateTime GetEventTime()
{
// Get the event source.
DateTime eTime = EventTime;
return eTime;
}
// Gets the event time.
public DateTime GetEventTimeUtc()
{
// Get the event source.
DateTime eTime = EventTimeUtc;
return eTime;
}
// Gets the event sequence.
public string GetEventMessage()
{
// Get the event message.
string eventMsg = Message;
return eventMsg;
}
// Gets the current application information.
public WebApplicationInformation GetEventAppInfo()
{
// Get the event message.
WebApplicationInformation appImfo =
ApplicationInformation;
return appImfo;
}
// Implements the ToString() method.
public override string ToString()
{
return base.ToString();
}
// Implements the ToString(bool, bool) method.
public string customToString(bool includeAppInfo,
bool includeCustomInfo)
{
return (
base.ToString(includeAppInfo,
includeCustomInfo));
}
// Gets the event identifier.
public Guid GetEventId()
{
Guid evId = EventID;
return evId;
}
//Formats Web request event information.
public override void FormatCustomEventDetails(
WebEventFormatter formatter)
{
base.FormatCustomEventDetails(formatter);
// Add custom data.
formatter.AppendLine("");
formatter.IndentationLevel += 1;
formatter.TabSize = 4;
formatter.AppendLine(
"*SampleWebBaseEvent Start *");
// Display custom event information.
formatter.AppendLine(customCreatedMsg);
formatter.AppendLine(customRaisedMsg);
formatter.AppendLine(firingRecordInfo);
formatter.AppendLine(
"* SampleWebBaseEvent End *");
formatter.IndentationLevel -= 1;
}
}
}
Imports System.Text
Imports System.Web
Imports System.Web.Management
Public Class SampleWebBaseEvent
Inherits System.Web.Management.WebBaseEvent
Implements System.Web.Management.IWebEventCustomEvaluator
Private customCreatedMsg, customRaisedMsg As String
' Store firing record info.
Private Shared firingRecordInfo As String
' Implements the IWebEventCustomEvaluator.CanFire
' method. It is called by the ASP.NET if this custom
' type is configured in the profile
' element of the healthMonitoring section.
Public Function CanFire( _
ByVal e As System.Web.Management.WebBaseEvent, _
ByVal rule As RuleFiringRecord) As Boolean _
Implements System.Web.Management.IWebEventCustomEvaluator.CanFire
Dim fireEvent As Boolean
Dim lastFired As String = _
rule.LastFired.ToString()
Dim timesRaised As String = _
rule.TimesRaised.ToString()
' Fire every other event raised.
fireEvent = _
IIf(rule.TimesRaised Mod 2 = 0, True, False)
If fireEvent Then
firingRecordInfo = String.Format( _
"Event last fired: {0}", lastFired) + _
String.Format( _
". Times raised: {0}", timesRaised)
Else
firingRecordInfo = String.Format( _
"Event not fired. Times raised: {0}", _
timesRaised)
End If
Return fireEvent
End Function 'CanFire
' Invoked in case of events identified only by
' their event code.
Public Sub New(ByVal msg As String, _
ByVal eventSource As Object, _
ByVal eventCode As Integer)
MyBase.New(msg, eventSource, eventCode)
' Perform custom initialization.
customCreatedMsg = String.Format( _
"Event created at: {0}", DateTime.Now.TimeOfDay.ToString())
End Sub
' Invoked in case of events identified by their
' event code.and related event detailed code.
Public Sub New(ByVal msg As String, _
ByVal eventSource As Object, _
ByVal eventCode As Integer, _
ByVal eventDetailCode As Integer)
MyBase.New(msg, eventSource, _
eventCode, eventDetailCode)
' Perform custom initialization.
customCreatedMsg = String.Format( _
"Event created at: {0}", DateTime.Now.TimeOfDay.ToString())
End Sub
' Raises the SampleWebBaseEvent.
Public Overrides Sub Raise()
' Perform custom processing.
customRaisedMsg = String.Format( _
"Event raised at: {0}", DateTime.Now.TimeOfDay.ToString())
' Raise the event.
MyBase.Raise()
End Sub
' Raises the SampleWebBaseEvent.
Public Sub CustomRaise(ByVal evnt _
As System.Web.Management.WebBaseEvent)
' Raise the event.
Raise(evnt)
End Sub
' Gets the event code.
Public Function GetEventCode(ByVal detail _
As Boolean) As Integer
Dim eCode As Integer
If Not detail Then
' Get the event code.
eCode = EventCode
' Get the detail event code.
Else
eCode = EventDetailCode
End If
Return eCode
End Function 'GetEventCode
' Gets the event sequence.
Public Function GetEventSequence() As Long
' Get the event sequence.
Dim eventSequence As Long = eventSequence
Return eventSequence
End Function 'GetEventSequence
' Gets the event source.
Public Function GetEventSource() As [Object]
' Get the event source.
Dim [source] As [Object] = Me.EventSource
Return [source]
End Function 'GetEventSource
' Gets the event time.
Public Function GetEventTime() As DateTime
' Get the event source.
Dim eTime As DateTime = EventTime
Return eTime
End Function 'GetEventTime
' Gets the event time.
Public Function GetEventTimeUtc() As DateTime
' Get the event source.
Dim eTime As DateTime = EventTimeUtc
Return eTime
End Function 'GetEventTimeUtc
' Gets the event sequence.
Public Function GetEventMessage() As String
' Get the event message.
Dim eventMsg As String = Message
Return eventMsg
End Function 'GetEventMessage
' Gets the current application information.
Public Function GetEventAppInfo() As WebApplicationInformation
' Get the event message.
Dim appImfo As WebApplicationInformation = _
ApplicationInformation
Return appImfo
End Function 'GetEventAppInfo
' Implements the ToString() method.
Public Overrides Function ToString() As String
Return MyBase.ToString()
End Function 'ToString
' Implements the ToString(bool, bool) method.
Public Function customToString(ByVal includeAppInfo As Boolean, _
ByVal includeCustomInfo As Boolean) As String
Return MyBase.ToString(includeAppInfo, includeCustomInfo)
End Function 'customToString
' Gets the event identifier.
Public Function GetEventId() As Guid
Dim evId As Guid = EventID
Return evId
End Function 'GetEventId
'Formats Web request event information.
Public Overrides Sub FormatCustomEventDetails( _
ByVal formatter As WebEventFormatter)
MyBase.FormatCustomEventDetails(formatter)
' Add custom data.
formatter.AppendLine("")
formatter.IndentationLevel += 1
formatter.TabSize = 4
formatter.AppendLine("*SampleWebBaseEvent Start *")
formatter.AppendLine("Custom information goes here")
formatter.AppendLine("* SampleWebBaseEvent End *")
' Display custom event timing.
formatter.AppendLine(customCreatedMsg)
formatter.AppendLine(customRaisedMsg)
formatter.IndentationLevel -= 1
End Sub
End Class
下列範例是組態檔的摘錄,可讓 ASP.NET 使用自訂事件。
<healthMonitoring enabled="true" heartBeatInterval="0">
<eventMappings>
<add name="SampleWebBaseEvent"
type="SamplesAspNet.SampleWebBaseEvent,
webbaseevent, Version=1.0.1573.24438, Culture=neutral,
PublicKeyToken=2f5f337ae5c9bdaa,
processorArchitecture=MSIL" />
</eventMappings>
<rules>
<add
name="CustomWebBaseEvent"
eventName="SampleWebBaseEvent"
provider="EventLogProvider"
profile="Critical"/>
</rules>
</healthMonitoring>
備註
ASP.NET 健康情況監視可讓生產與作業人員管理已部署的 Web 應用程式。 System.Web.Management命名空間包含負責封裝應用程式健康狀態資料的健全狀況事件種類,以及負責處理此資料的提供者類型。 它也包含支援類型,可協助管理健康情況事件。
類別 WebBaseEvent 是所有 ASP.NET 健全狀況監視事件種類的繼承樹狀目錄。 它會使用 WebApplicationInformation 類別來取得其衍生類別可用的應用程式資訊。
注意
在大部分情況下,您將能夠使用實作的 ASP.NET 健康情況監視類型,而且您會在組態區段中指定值 <healthMonitoring>
來控制健康情況監視系統。 您也可以衍生自健康情況監視類型,以建立您自己的自訂事件和提供者。 如需衍生自 類別的 WebBaseEvent 範例,請參閱本主題中提供的範例。
給繼承者的注意事項
當您將自訂事件資訊格式化以顯示時,請覆寫 FormatCustomEventDetails(WebEventFormatter) 方法,而不是 ToString 方法。 這可避免覆寫或竄改敏感性系統資訊。
您為自訂事件指定的事件程式碼必須大於 WebExtendedBase 。
建構函式
WebBaseEvent(String, Object, Int32, Int32) |
使用提供的參數來初始化 WebBaseEvent 類別的新執行個體。 |
WebBaseEvent(String, Object, Int32) |
使用提供的參數來初始化 WebBaseEvent 類別的新執行個體。 |
屬性
ApplicationInformation |
取得 WebApplicationInformation 物件,該物件含有關於目前監視中應用程式的資訊。 |
EventCode |
取得與事件關聯的代碼值。 |
EventDetailCode |
取得事件詳細資料代碼。 |
EventID |
取得與此事件相關聯的識別項。 |
EventOccurrence |
取得計數器,表示事件發生的次數。 |
EventSequence |
取得應用程式引發此事件的次數。 |
EventSource |
取得引發事件的物件。 |
EventTime |
取得事件引發的時間。 |
EventTimeUtc |
取得事件引發的時間。 |
Message |
取得描述事件的訊息。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
FormatCustomEventDetails(WebEventFormatter) |
提供事件資訊的標準格式。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IncrementPerfCounters() |
在內部使用,以逐步遞增效能計數器。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Raise() |
告知設定的提供者事件已發生來引發事件。 |
Raise(WebBaseEvent) |
告知設定的提供者事件已發生來引發指定的事件。 |
ToString() |
針對顯示用途,格式化事件資訊。 |
ToString(Boolean, Boolean) |
針對顯示用途,格式化事件資訊。 |