WebEventProvider 类

定义

为非缓冲事件提供程序提供基类。Provides the base class for non buffered event providers.

public ref class WebEventProvider abstract : System::Configuration::Provider::ProviderBase
public abstract class WebEventProvider : System.Configuration.Provider.ProviderBase
type WebEventProvider = class
    inherit ProviderBase
Public MustInherit Class WebEventProvider
Inherits ProviderBase
继承
WebEventProvider
派生

示例

下面的代码示例演示如何从 WebEventProvider 类派生,以创建一个自定义提供程序,该提供程序将配置的事件写入本地文件,必须为该文件授予适当的访问权限。The following code example shows how to derive from the WebEventProvider class to create a custom provider that writes the configured events to a local file for which appropriate access rights must be granted. 此自定义提供程序示例很简单,其主要目的是让开发人员完全控制其基本机制。This custom provider example is simple and its main intent is to give you as developer full control of its basic mechanisms. 在实际方案中,可以使用此提供程序,特别是 BufferedWebEventProvider中提供的示例缓冲提供程序,作为对应用程序行为的初步探查。In a real-world scenario, you could use this provider and especially the example buffered provider available at BufferedWebEventProvider, as a preliminary probe into the behavior of an application. 这可以帮助你在设计阶段了解可用的信息;然后,你可以将此信息定向到更复杂的提供程序。This can help you during the design stage to gain an understanding of the information available; then later you can direct this information to a more complex provider.

下面的配置文件节选显示了一个 healthMonitoring 节配置,该配置使 ASP.NET 可以使用上面定义的自定义提供程序来处理所有的运行状况监视事件。The following configuration file excerpt shows a healthMonitoring section configuration that enables ASP.NET to use the custom provider defined above to process all health-monitoring events.

<healthMonitoring   
  heartBeatInterval="0"   
  enabled="true">  
  
    <providers>  
  
      <add name="SampleWebEventProvider"   
        type="SamplesAspNet.SampleEventProvider,webeventprovider, Version=1.0.1773.33989, Culture=neutral, PublicKeyToken=cf85aa6c978d9dea, processorArchitecture=MSIL" />  
  
    </providers>  
  
    <rules>  
  
      <rule   
        name="Custom Event Provider"  
        eventName="All Events"  
        provider="SampleWebEventProvider"  
        profile="Default" />  
    </rules>  
  
</healthMonitoring>  

using System;
using System.Text;
using System.IO;
using System.Web.Management;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;


namespace SamplesAspNet
{
  // Implements a custom event provider.
    public class SampleEventProvider : 
        System.Web.Management.WebEventProvider
    {

        // The local path of the file where
        // to store event information.
        private string logFilePath;
    
        // The current number of buffered messages 
        private int msgCounter;

        // The max number of messages to buffere.
        private int maxMsgNumber;

        // The message buffer.
        private System.Collections.Generic.Queue
            <WebBaseEvent> msgBuffer = 
            new Queue<WebBaseEvent>();

        // Initializes the provider.
        public SampleEventProvider(): base()
        {

            // Initialize the local path of the file 
            // that holds event information.
            logFilePath = "C:/test/log.doc";

            // Clear the message buffer.
            msgBuffer.Clear();

            // Initialize the max number of messages
            // to buffer.
            maxMsgNumber = 10;

            // More custom initialization goes here.

        }


        // Flush the input buffer if required.
        public override void Flush()
        {
            // Create a string builder to 
            // hold the event information.
            StringBuilder reData = new StringBuilder();

            // Store custom information.
            reData.Append("SampleEventProvider processing." +
                Environment.NewLine);
            reData.Append("Flush done at: {0}" +
                DateTime.Now.TimeOfDay.ToString() +
                Environment.NewLine);
            
            foreach (WebBaseEvent e in msgBuffer)
            {
                // Store event data.
                reData.Append(e.ToString());
            }

            // Store the information in the specified file.
            StoreToFile(reData, logFilePath, FileMode.Append);

            // Reset the message counter.
            msgCounter = 0;
            
            // Clear the buffer.
            msgBuffer.Clear();

        }


        // Shutdown the provider.
        public override void Shutdown()
        {
            Flush();
        }


        // Process the event that has been raised.
        public override void ProcessEvent(WebBaseEvent raisedEvent)
        { 
            if (msgCounter < maxMsgNumber)
            {
                // Buffer the event information.
                msgBuffer.Enqueue(raisedEvent);
                // Increment the message counter.
                msgCounter += 1;
            }
            else
            {
                // Flush the buffer.
                Flush();
            }
        }



        // Store event information in a local file.
        private void StoreToFile(StringBuilder text, 
            string filePath, FileMode mode)
        {
            int writeBlock;
            int startIndex;

            try
            {

                writeBlock = 256;
                startIndex = 0;

                // Open or create the local file 
                // to store the event information.
                FileStream fs = new FileStream(filePath, 
                    mode, FileAccess.Write);

                // Lock the file for writing.
                fs.Lock(startIndex, writeBlock);

                // Create a stream writer
                StreamWriter writer = new StreamWriter(fs);

                // Set the file pointer to the current 
                // position to keep adding data to it. 
                // If you want to rewrite the file use 
                // the following statement instead.
                // writer.BaseStream.Seek (0, SeekOrigin.Begin);
                writer.BaseStream.Seek(0, SeekOrigin.Current);

                //If the file already exists it must not 
                // be write protected otherwise  
                // the following write operation fails silently.
                writer.Write(text.ToString());

                // Update the underlying file
                writer.Flush();

                // Unlock the file for other processes.
                fs.Unlock(startIndex, writeBlock);

                // Close the stream writer and the underlying file     
                writer.Close();

                fs.Close();
            }
            catch (Exception e)
            {
                throw new Exception(
                    "SampleEventProvider.StoreToFile: " 
                    + e.ToString());
            }
        }

    }

}

Imports System.Text
Imports System.IO
Imports System.Web.Management
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.Web



' Implements a custom event provider.

Public Class SampleEventProvider
    Inherits System.Web.Management.WebEventProvider
    
    ' The local path of the file where
    ' to store event information.
    Private logFilePath As String
    
    ' The current number of buffered messages 
    Private msgCounter As Integer
    
    ' The max number of messages to buffere.
    Private maxMsgNumber As Integer
    
    ' The message buffer.
    '  private System.Collections.Generic.Queue
    Private msgBuffer _
    As System.Collections.Generic.Queue( _
    Of System.Web.Management.WebBaseEvent) = _
    New System.Collections.Generic.Queue( _
    Of System.Web.Management.WebBaseEvent)


    ' Initializes the provider.
    Public Sub New() 
        
        ' Initialize the local path of the file 
        ' that holds event information.
        logFilePath = "C:/test/log.doc"
        
        ' Clear the message buffer.
        msgBuffer.Clear()
        
        ' Initialize the max number of messages
        ' to buffer.
        maxMsgNumber = 10
    
    End Sub
     
    ' More custom initialization goes here.
    
    ' Flush the input buffer if required.
    Public Overrides Sub Flush() 
        ' Create a string builder to 
        ' hold the event information.
        Dim reData As New StringBuilder()
        
        ' Store custom information.
        reData.Append( _
        "SampleEventProvider processing." + _
        Environment.NewLine)

        reData.Append( _
        "Flush done at: {0}" + _
        DateTime.Now.TimeOfDay.ToString() + _
        Environment.NewLine)
        
        Dim e As WebBaseEvent
        For Each e In  msgBuffer
            ' Store event data.
            reData.Append(e.ToString())
        Next e
        
        ' Store the information in the specified file.
        StoreToFile(reData, logFilePath, FileMode.Append)
        
        ' Reset the message counter.
        msgCounter = 0
        
        ' Clear the buffer.
        msgBuffer.Clear()
    
    End Sub
     
    ' Shutdown the provider.
    Public Overrides Sub Shutdown() 
        Flush()
    
    End Sub
    
    ' Process the event that has been raised.
    Public Overrides Sub ProcessEvent( _
    ByVal raisedEvent As WebBaseEvent)

        If msgCounter < maxMsgNumber Then
            ' Buffer the event information.
            msgBuffer.Enqueue(raisedEvent)
            ' Increment the message counter.
            msgCounter += 1
        Else
            ' Flush the buffer.
            Flush()
        End If

    End Sub
    
    
    ' Store event information in a local file.
    Private Sub StoreToFile( _
    ByVal [text] As StringBuilder, _
    ByVal filePath As String, _
    ByVal mode As FileMode)
        Dim writeBlock As Integer
        Dim startIndex As Integer

        Try

            writeBlock = 256
            startIndex = 0

            ' Open or create the local file 
            ' to store the event information.
            Dim fs As New FileStream( _
            filePath, mode, FileAccess.Write)

            ' Lock the file for writing.
            fs.Lock(startIndex, writeBlock)

            ' Create a stream writer
            Dim writer As New StreamWriter(fs)

            ' Set the file pointer to the current 
            ' position to keep adding data to it. 
            ' If you want to rewrite the file use 
            ' the(following) statement instead.
            ' writer.BaseStream.Seek (0, SeekOrigin.Begin);
            writer.BaseStream.Seek(0, SeekOrigin.Current)

            'If the file already exists it must 
            'not be write protected, otherwise  
            'the following write operation fails silently.
            writer.Write([text].ToString())

            ' Update the underlying file
            writer.Flush()

            ' Unlock the file for other processes.
            fs.Unlock(startIndex, writeBlock)

            ' Close the stream writer and the underlying file     
            writer.Close()

            fs.Close()
        Catch e As Exception
            Throw New Exception( _
            "SampleEventProvider.StoreToFile: " + _
            e.ToString())
        End Try

    End Sub
End Class


注解

ASP.NET health monitoring 允许生产和运营人员管理已部署的 Web 应用程序。ASP.NET health monitoring allows production and operations staff to manage deployed Web applications. System.Web.Management 命名空间包含负责打包应用程序运行状况数据的运行状况事件类型,以及负责处理此数据的提供程序类型。The System.Web.Management namespace contains the health event types responsible for packaging application health-status data and the provider types responsible for processing this data. 它还包含在运行状况事件的管理过程中可帮助的支持类型。It also contains supporting types that help during the management of health events.

如果要自定义运行状况事件处理,可从 WebEventProvider 类派生,以创建自己的自定义提供程序。If you want to customize the health-event processing, you can derive from the WebEventProvider class to create your own custom provider.

备注

在大多数情况下,你将能够使用已实现的 ASP.NET health 监视类型,并且将通过在 "healthMonitoring 配置" 部分中指定值来控制运行状况监视系统。In most cases you will be able to use the ASP.NET health-monitoring types as implemented, and you will control the health-monitoring system by specifying values in the healthMonitoring configuration section. 还可以从运行状况监视类型派生,以创建自己的自定义事件和提供程序。You can also derive from the health-monitoring types to create your own custom events and providers. 有关从 WebEventProvider 类派生的示例,请参阅本主题中提供的示例。For an example of deriving from the WebEventProvider class, see the example provided in this topic.

构造函数

WebEventProvider()

初始化 WebEventProvider 类的新实例。Initializes a new instance of the WebEventProvider class.

属性

Description

获取一条简短的易懂描述,它适合在管理工具或其他用户界面 (UI) 中显示。Gets a brief, friendly description suitable for display in administrative tools or other user interfaces (UIs).

(继承自 ProviderBase)
Name

获得一个友好名称,用于在配置过程中引用提供程序。Gets the friendly name used to refer to the provider during configuration.

(继承自 ProviderBase)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Flush()

将事件从提供程序缓冲区移到事件日志中。Moves the events from the provider's buffer into the event log.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

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

初始化配置生成器。Initializes the configuration builder.

(继承自 ProviderBase)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ProcessEvent(WebBaseEvent)

处理传递给提供程序的事件。Processes the event passed to the provider.

Shutdown()

执行与关闭提供程序相关联的任务。Performs tasks associated with shutting down the provider.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅