How to use Health Monitoring in WCF

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

You can use Health Monitoring to log custom events in your service based on business logic. You can use health monitoring to Instrument your application and monitor user management events around authentication and authorization. This instrumentation can help you to detect and react to potentially suspicious behavior. It also enables you to gather operations data; for example, to track who is accessing your application and when user account passwords need to be reset.

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.
  1. 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.

Additional Resources

Last edited Jun 13, 2008 at 5:29 PM by prashantbansode, version 1

Comments

No comments yet.