How to use Health Monitoring in WCF

J.D. Meier, Jason Taylor, Prashant Bansode, Carlos Farre, Madhu Sundararajan, Steve Gregersen.

Use the following steps to configure your WCF service to use Health Monitoring:
  1. Create a custom health monitoring event.
  2. Configure your WCF service for health monitoring.
  3. Instrument your application to raise a custom event.
  4. Create a custom health monitoring event

Create a Custom user management web event, by creating a class library and then create a class that inherits from WebAuditEvent.
using System.Web.Management;

public class MyEvent : WebAuditEvent
{

    public MyEvent(string msg, object eventSource, int eventCode)
        : base(msg, eventSource, eventCode)
    {
    }

    public MyEvent(string msg, object eventSource, int eventCode, int eventDetailCode)
        : base(msg, eventSource, eventCode, eventDetailCode)
    {
    }
    public override void FormatCustomEventDetails(WebEventFormatter formatter)
    {
        base.FormatCustomEventDetails(formatter);

        // Display some custom event message
        formatter.AppendLine("Some Critical Event Fired");
   }
}
  1. Configure your WCF Service for Health Monitoring
Add a health monitoring element to your configuration file as follows:
…
<system.web>
	<healthMonitoring>
		<eventMappings>
			<add name="Some Custom Event" type="MyEventLibrary.MyEvent, MyEventLibrary"/>
		</eventMappings>
		<rules>
			<add name="Custom event" eventName="Some Custom Event" provider="EventLogProvider" minInterval="00:00:01"/>
		</rules>
	</healthMonitoring>
</system.web>
…
  1. Instrument your application to raise a custom event
Instrument the WCF Service by raising the custom event in a service contract.
[OperationContract]
string InvokeCriticalEvent();

public string InvokeCriticalEvent()
{
    MyEvent obj = new MyEvent("Invoking Some Custom Event", this, WebEventCodes.WebExtendedBase + 1);
    obj.Raise();
    return "Critical event invoked";
}

When these steps are complete you can verify that the custom events are in the system event log after calling the service method from a test client.

Last edited May 7, 2008 at 11:38 PM by prashantbansode, version 1

Comments

No comments yet.