Custom Web Events


It's really trivial to add custom data that you'd like to be logged through the Web event system. Just derive a class from one of the existing WebBaseEvent-derived classes or from WebBaseEvent itself, and use constructor arguments to specify the message and event code you want to log. Listing 7-9 illustrates a simple example.

Listing 7-9. A custom Web event

using System; using System.Web.Management; public enum MyEventCodes {     LowInventory = 0,     InvalidInput = 1,     // ... } public class CustomEvent : WebBaseEvent {     public CustomEvent(string message,                        object eventSource,                        MyEventCodes myEventCode)         : base(message, eventSource,                WebEventCodes.WebExtendedBase + (int)myEventCode)     {} } 

To come up with unique event codes that won't step on ones that ASP.NET provides, note how this example starts at the integer value defined by WebEventCodes.WebExtendedBase and goes up from there. Listing 7-10 shows some code that fires this Web event to indicate that inventory is getting low.

Listing 7-10. Firing a custom Web event

CustomEvent e =     new CustomEvent("Low Inventory", this, MyEventCodes.LowInventory); e.Raise(); 

By default, the only <eventMapping> that will match this custom event is All Events, because it matches the base class WebBaseEvent. So when you start using custom events, you'll likely want to create your own event mappings to match your custom events more specifically. Remember how event mappings can match events based on type, so if you're going to make extensive use of custom events, you should consider building a hierarchy of event classes that will simplify event mapping, as the ASP.NET team did with the default event classes (see Figure 7-1). Be sure to choose the base class for any given custom Web event carefully, and wherever possible derive from the existing Web event classes; this way, your events will naturally fit into the default event mappings and be an intuitive addition to the built-in events. As you'll see later on, some of the ASP.NET infrastructure reasons about Web events by looking at their base class (for example, a Web event is considered an "error" event if it derives from WebBaseErrorEvent).

Some custom events might need to carry along extra information in addition to the message string. This is easy to do, but you'll need to override the FormatCustomEventDetails method to serialize this other data to the provider. If your event is being recorded by a buffered provider, this method will be called by a worker thread, and you won't have access to HttpContext.Current, so if you need to scrape out the request's details or any other thread-sensitive resources, be sure to do that during the initialization of your event. By the time FormatCustomEventDetails is called, it may be too late to get that extra data you need.

Listing 7-11 is a custom event that includes the name of the client making the request. Note how the class gathers all the data it needs in its constructor.

Listing 7-11. Managing custom data in a custom Web event

public class CustomEvent : WebBaseEvent {     public CustomEvent(string message,                        object eventSource,                        MyEventCodes myEventCode)         : base(message, eventSource,                WebEventCodes.WebExtendedBase + (int)myEventCode)     {         userName = HttpContext.Current.User.Identity.Name;     }     public override void FormatCustomEventDetails(              WebEventFormatter formatter) {         base.FormatCustomEventDetails(formatter);         formatter.AppendLine("Client name: " + userName);     }     readonly string userName; } 




Essential ASP. NET 2.0
Essential ASP.NET 2.0
ISBN: 0321237706
EAN: 2147483647
Year: 2006
Pages: 104

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net