Click here to Skip to main content
15,881,881 members
Articles / WCF

4 Steps to Enable Instrumentation in WCF

,
Rate me:
Please Sign up or sign in to vote.
4.52/5 (14 votes)
28 Mar 2010CPOL4 min read 57.1K   320   34   11
4 steps to enable instrumentation in WCF

Table of Contents

Introduction and Goal

Many times, we would like to monitor events of WCF application in production environment. We would like to monitor events like errors, security audits, performance, etc. This can be achieved by extending the ASP.NET health monitoring system in WCF. The health monitoring system is also termed as instrumentation.

The Event Provider Model

Instrumentation is provided using the event provider model. Events are notifications which you receive from the WCF application which can be a security event like password change, UI click events, or exception events like application level errors. These events are captured by the provider and routed to some source like event viewer, SQL Server, etc.

Image 1

Both events and provider are specified in the web.config file. The eventMappings element is where you specify your provider and ‘rules’ elements help you tie up the event with the provider.

XML
<healthMonitoring>
<eventMappings>...</eventMappings>
<rules>...</rules>
</healthMonitoring>

What Will We Do in this Article?

In this article, we will create a simple audit function which will help us to track all calls made to the WCF service in to event viewer. So any calls by the WCF client will be tracked and audited in to the event viewer. For every call, we will be tracking details like number of threads, working sets, app domains, when the message was created and when was it raised. Below is the snippet for the same which will be tracked in the event viewer.

***************start health monitoring event*******************
message created at:Event Created at :3/14/2010 11:32:37 AM
message raised at:Event Created at :3/14/2010 11:32:37 AM
Heap size 3480664
Number of threads 19
Number of Working sets 32165888
Number of domains 1
Request rejected 0******************End Health Monitoring event*********************

Step 1: Create the Main Event Class

The first step is to create a class which will help us to track the calls made to the WCF service. This class needs to be inherited from WebAuditEvent class. This class helps us to track audit events, generate information about security related operation and provide both success and failure audit events.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Management;

namespace healthmonitering
{
    public class CustomAudit:WebAuditEvent
    {
    }
}

In the same class, let's add 3 private fields, msgcreated , msgraised and WebProcessStatistics. By using msgcreated and msgraised, we can track at what time event is created and raised. WebProcessStatistics will provide information for assessing the health of a running process.

C#
private string msgcreated = string.Empty;
private string msgraised = string.Empty;
private static WebProcessStatistics processStatistics;

Implement the necessary public constructors that call the protected equivalents in the parent WebAuditEvent class. Base keyword is used to access the member of base class within the derived class as shown in the below code snippet. Note that we have created the WebProcessStatistics object and set it to the private member variable.

C#
public CustomAudit(string message, object eventsource, int eventcode)
       : base(message, eventsource, eventcode)
{
    msgcreated = string.Format("Event Created at :{0}", EventTime.ToString());
    processStatistics = new WebProcessStatistics();
} 

In both the constructors, we are checking at what time event is created. Now override the Raise method as shown in the below code snippet.

C#
public override void Raise()
{
    msgraised = string.Format("Event Created at :{0}", EventTime.ToString());
    base.Raise();
}

Override the FormatCustomEventDetails method with the message we want to log in the event viewer. Note, we have used WebProcessStatistics to get information like heap size, number of threads, number of working sets, number of domains and Request rejected.

C#
public override void FormatCustomEventDetails(WebEventFormatter formatter)
{
    formatter.AppendLine("");
    formatter.IndentationLevel += 1;
    formatter.AppendLine
	("***************start health monitoring event*******************");
    formatter.AppendLine(string.Format("message created at:{0}",msgcreated));
    formatter.AppendLine(string.Format("message raised at:{0}", msgraised));
    formatter.AppendLine(string.Format
	("Heap size {0}", processStatistics.ManagedHeapSize.ToString()));
    formatter.AppendLine(string.Format
	("Number of threads {0}", processStatistics.ThreadCount.ToString()));
    formatter.AppendLine(string.Format
	("Number of Working sets {0}", processStatistics.WorkingSet.ToString()));
    formatter.AppendLine(string.Format
	("Number of domains {0}", processStatistics.AppDomainCount.ToString()));
    formatter.AppendLine(string.Format
	("Request rejected {0}", processStatistics.RequestsRejected.ToString())); ;
    formatter.AppendLine
	("******************End Health Monitoring event*********************");
    formatter.IndentationLevel -= 1;
}

In the Raise method, we are checking at what time event raised and in FormatCustomEventDetails, we are appending the result in the audit event.

Step 2: Create the WCF Service and Change web.config

C#
namespace WcfService3
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string Audit();
    }
}

In the Audit function, we are going to implement our health monitoring functionality in health operation contract.

In the Service1.svc.cs class, implement the Audit function by creating the CustomAudit object and calling the Raise function as shown in the below code snippet:

C#
public class Service1 : IService1
{
    public string Audit()
    {
        Healthmonitering.CustomAudit webevent = 
		new Healthmonitering.CustomAudit("Some on called", 
                    this, WebEventCodes.WebExtendedBase + 1);
        webevent.Raise();
        return "Event Audited";
    }
}

In the constructor, we are sending message, event source, and event code as a parameter.

Now go to the web.config and under system.web element, add the healthmonitoring tag. Specify CustomAudit class in the eventMappinga element and mapping of the class with event viewer in the rules element tag as shown in the below code snippet.

XML
<healthMonitoring>
<eventMappings>
<add name="healthmonitering" type="Healthmonitering.CustomAudit "/>
</eventMappings>
<rules>
<add name="healthmonitering" eventName="healthmonitering" 
	provider="EventLogProvider" minInterval="00:00:01"/>
</rules>
</healthMonitoring>

Step 3: Consume the WCF Service in the ASPX Client

So let’s add an ASPX button, consume the client service and call the Audit function in the button click event as shown in the below code snippets:

ASP.NET
<asp:Button ID="Button1" runat="server" Text="Invoke" onclick="Button1_Click" />

Now in the button click event, write these lines:

C#
protected void Button1_Click(object sender, EventArgs e)
{
    ServiceReference1.Service1Client proxy = 
		new WebApplication1.ServiceReference1.Service1Client();
    string result = proxy.Audit();
    Response.Write(result);
}

Here we are creating the proxy, invoking the Audit method and displaying the result of the Audit method.

Step 4: See the Audit Data in Event Viewer

Run the web application which is consuming the WCF service and press the button to invoke the WCF audit function. The Audit function internally calls the Raise event which logs the message in an event viewer. So go to Start->run->type eventvwr. It contains information like Heap size, Number of threads, Number of Working sets, Number of domains and Request rejected as shown in the below figure:

Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Written By
Software Developer Adea
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs this will work with self hosted service? Pin
TanyaKogan14-Dec-11 1:01
TanyaKogan14-Dec-11 1:01 
AnswerRe: Is this will work with self hosted service? Pin
Shivprasad koirala14-Dec-11 1:09
Shivprasad koirala14-Dec-11 1:09 
GeneralRe: Is this will work with self hosted service? Pin
TanyaKogan14-Dec-11 1:13
TanyaKogan14-Dec-11 1:13 
GeneralRe: Is this will work with self hosted service? Pin
Shivprasad koirala14-Dec-11 2:21
Shivprasad koirala14-Dec-11 2:21 
GeneralRe: Is this will work with self hosted service? Pin
TanyaKogan14-Dec-11 2:36
TanyaKogan14-Dec-11 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.