Sdílet prostřednictvím


How to: Implement the Health Monitoring Custom Provider Example

The example in the topic Custom ASP.NET Health Monitoring Provider Example illustrates how to create a custom provider that processes ASP.NET health events. The code example shows how to derive from the BufferedWebEventProvider class to create a custom provider that writes the event information to a local file.

The example is deliberately simple. The intent is to give you full control of a custom provider's basic mechanisms. In a real-world scenario, you could use the example code as a preliminary probe into the behavior of an application to help you understand the information provided by ASP.NET health monitoring.

The example provider demonstrates the following features:

  • Building a custom provider by inheriting from the BufferedWebEventProvider. This provider includes the ability to hold event information based on various factors (such as a specified time interval) before logging it.

  • Configuring a custom provider.

  • Interfacing with the Windows Event Viewer.

Running the example requires the following:

To build the custom event provider

  • Compile the custom event provider as a library and place the library in your ASP.NET application's Bin directory, or strongly name the assembly and place it in the global assembly cache (GAC).

    The following command example shows how you can compile the example using the command-line compiler.

    vbc /out:<example_name>.dll /t:library <example_name>.vb /r:System.Web.dll /r:System.Configuration.dll /r:<required namespace>
    
    csc /out:<example_name>.dll /t:library <example_name>.cs /r:System.Web.dll /r:System.Configuration.dll  /r:<required namespace>
    
    NoteNote

    If you cannot execute the compiler command, you must add the .NET Framework installation path to the Windows PATH variable before running the command. In Windows, right-click My Computer, click Properties, click the Advanced tab, and then click the Environment Variables button. In the System variables list, double-click the Path variable. In the Variable value text box, add a semicolon (;) to the end of the existing values in the text box, and then type the path of your .NET Framework installation. The .NET Framework is usually installed in the Windows installation folder at \Microsoft.NET\Framework\versionNumber.

To configure the application to use the example

  1. If your application already contains a file named Web.config in the root folder, open it. Otherwise, create a text file named Web.config and add the following text.

    <?xml version="1.0"?>
    <configuration xmlns="https://schemas.microsoft.com/.NetConfiguration/v2.0">
      <system.web>
      </system.web>
    </configuration>
    
  2. Inside the system.web tags, add the following healthMonitoring element.

    <healthMonitoring
        heartBeatInterval="0" enabled="true">
      <bufferModes>
        <add name="Custom Notification"
          maxBufferSize="10"
          maxFlushSize="5"
          urgentFlushThreshold="2"
          regularFlushInterval="Infinite"
          urgentFlushInterval="00:00:30"
        />
      </bufferModes>
      <providers>
        <add name="SampleEventProvider" type="Samples.AspNet.Management.SampleBufferedEventProvider" 
        buffer="true"
        bufferMode="Custom Notification" />
      </providers>
      <profiles>
        <add name="Custom"
          minInstances="1"
          maxLimit="Infinite"
          minInterval="00:00:00" />
      </profiles>
      <rules>
        <add name="Custom Event Provider"
          eventName="All Events"
          provider="SampleEventProvider"
          profile="Custom" />
      </rules>
    </healthMonitoring>
    

    The settings in the healthMonitoring element use a providers element to add the custom provider and specify a profiles element that (in this case) protects the server by limiting the number of times the event can be raised. The example also includes a rules element to associate an event with the profile and the provider.

    The type attribute can list just the class name as it does in the previous code example, or it can list a fully qualified type, as in the following example:

    type="Samples.AspNet.Management.SampleWebRequestEvent,
    Sample.SampleCustomEventProvider,Version=1.0.0.0,Culture=neutral, 
    PublicKeyToken=xxxxxxxxxxxx"
    
    NoteNote

    The fully qualified type is required only if the class has been installed in the GAC or in the Bin directory.

To test the custom Web event

  1. In your browser, request any page from the Web application.

    Running any page in the application raises the custom event.

  2. To view your custom event data, open the log file specified by the logFilePath variable in the code.

See Also

Reference

healthMonitoring Element (ASP.NET Settings Schema)
EventLogWebEventProvider

Concepts

Custom ASP.NET Health Monitoring Provider Example

Other Resources

ASP.NET Health Monitoring
ASP.NET Web Applications