WebBaseEvent and WebEventProviders

Published Wed, Sep 15 2004 23:57 | girishb

Web Events and Providers

I attended Jeff Prosise's talk about ASP.NET 2.0 today at the Boston dot net group. That was totally awesome. What caught my attention was the whole <healthMonitoring> stuff that is out there with Whidbey. That thing is extremely cool. The really neat part is that you can get this to work with a few lines of coding.

 

There are many things you can do with health monitoring section within web.config. You can set up email notifications when things happen, events to be logged to NT event logs, heart beats to make sure that the system is alive and well. The interesting part though, is that you can write your own events to be raised and your own providers to hook up to these. Once you get them to work correctly, you can mix and match these things to your heart’s content. For e.g., you might setup your own “web event” which is triggered after every hundred sales of your widget and hook it to the standard email provider to send you or your boss an email or you can write your own web event provider which can hook into critical events (such as process recycles) and do some stuff (such as paging you). This is all very cool and you will learn more as you go through the immersion process of ASP.NET 2.0.

 

So, of course, I had to write one of my own. Since, I wanted to get this to work within 2 hours, there was no *real* problem to solve. So, I put a button on a page and wanted the clicks on that button to raise my web event and be handled by my event provider. As you can see the whole sample is pretty lame. But, it is written for demo purposes only.

 

I started off by creating a new web site. Edited it to have a Button and double clicked to handle the click event. So far, it is as dumb as it goes.

 

Next I created a class derived off of WebBaseEvent class. This is very simple and does not do much. I created this for the sole purpose of having a “custom” web event that can be hooked up in <healthMonitoring> section of web.config.

 

using System;

using System.Web.Management;

using System.Text;

using System.Web;

 

       // Implements a custom WebManagementEvent class.

public class MyWebEvent : WebBaseEvent

{

       public MyWebEvent (string msg, object eventSource, int eventCode) :

      base(msg, eventSource, eventCode)

       {

       }

      

 

       // Invoked in case of events identified by their event code.and

       // related event detailed code.

       public MyWebEvent(string msg, object eventSource, int eventCode, int eventDetailCode) :

      base(msg, eventSource, eventCode, eventDetailCode)

       {

       }

 

       public override void Raise()

       {

              base.Raise();

       }

 

       // Raises the SampleWebBaseEvent.

       public void CustomRaise(WebBaseEvent evnt)

       {

              Raise(evnt);

       }

}     

 

After this, I need to implement the Provider as well so that I can hook it up too. Following is the implementation of such a provider. You derive a class off of WebEventProvider (which is derived off of ProviderBase).

All this does is to log the event that comes across in ProcessEvent method to a text file.

 

using System;

using System.Web.Management;

using System.IO;

using System.Collections.Specialized;

 

/// <summary>

/// Summary description for MyWebEventProvider

/// </summary>

public class MyWebEventProvider:WebEventProvider

{

       string _providerName = "";

       public override void Initialize(string name, NameValueCollection config)

       {

              _providerName = name;

       }

 

       public override void Flush()

       {

             

       }

 

       public override void ProcessEvent(WebBaseEvent raisedEvent)

       {

              using (StreamWriter st = File.AppendText(@"C:\temp\Myevents.txt"))

              {

                     st.WriteLine("{0}\t{1}\t{2}\t{3}\t", raisedEvent.EventCode, raisedEvent.EventId, raisedEvent.EventTime,raisedEvent.Message);

                     st.Flush();

              }

       }

 

       public override void Shutdown()

       {

       }

}

Now that we have a Provider and an Event class, Lets hook them all up.

 

I tried to put them in the “Code” directory of Whidbey to get them to compile automatically, but for some reason the references were failing. So, I just built an assembly out of these files and put them in the “Bin” directory as we usually do.

 

Here is a Page class which basically raises the event with some message. The only interesting part is that we should make sure that the EventCode is greater than WebEventCodes.WebExtendedBase. This should be handled by the Custom web event itself but since I am lazy and want this to work, I just coded it in.

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Web.Management;

 

public partial class Default_aspx

{

       void Button1_Click(object sender, EventArgs e)

       {

              MyWebEvent mwe = new MyWebEvent("Button1 as clicked", this, WebEventCodes.WebExtendedBase + 1);

              mwe.CustomRaise(mwe);

       }

 

}

 

<%@ Page Language="C#" CompileWith="Default.aspx.cs" ClassName="Default_aspx" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

              &nbsp;

              <asp:Button ID="Button1" Runat="server" Text="Hit Me" OnClick="Button1_Click" />

   

    </div>

    </form>

</body>

</html>

 

 

After this, the only thing remaining is to get them all to play nicely together.

You need to put the puzzle pieces together in web.config’s healthMonitoring section.

 

<healthMonitoring enabled="true">

       <providers>

<add name="MyOwnProvider"

type="MyWebEventProvider,MyWebEventProvider"

/>

       </providers>

<eventMappings>

<add name="MyEvent"

type="MyWebEvent,MyWebEvent"

/>

</eventMappings>

       <rules>

       <add name="My Custom events"

eventName="MyEvent"

provider="MyOwnProvider"

minInterval="00:00:01"/>

       </rules>

</healthMonitoring>

 

So, what are we doing in this section?

  1. Turn on health monitoring. (enabled = ‘true’)
  2. Add our custom provider. <providers> section has that. The type handles type of the provider and the assembly. The samples had the fully qualified name.
  3. Add our events. <eventMappings> has that. Same as above.
  4. Add the rule to connect the providers to go with the event. <rules> covers that.

 

The only thing that caused me a bit of a problem was the WebExtendedBase being the base for any event codes. Other than that, this was a breeze.

 

All in all, a very cool feature.Hopefully, I did not confuse you more than usual.

 

Comments

# TrackBack said on September 19, 2004 5:17 AM:

# TrackBack said on September 21, 2004 4:18 AM:

# TrackBack said on October 5, 2004 6:18 AM:

# TrackBack said on December 9, 2004 12:38 PM:

# TrackBack said on December 9, 2004 12:45 PM:

# TrackBack said on February 23, 2005 11:21 PM:

# TrackBack said on February 23, 2005 11:22 PM: