Monitoring your WCF Web Apis with AppFabric
Posted
by cibrax
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by cibrax
Published on Mon, 07 Mar 2011 17:13:18 GMT
Indexed on
2011/03/08
0:11 UTC
Read the original article
Hit count: 454
The other day, Ron Jacobs made public a template in the Visual Studio Gallery for enabling monitoring capabilities to any existing WCF Http service hosted in Windows AppFabric. I thought it would be a cool idea to reuse some of that for doing the same thing on the new WCF Web Http stack.
Windows AppFabric provides a dashboard that you can use to dig into some metrics about the services usage, such as number of calls, errors or information about different events during a service call. Those events not only include information about the WCF pipeline, but also custom events that any developer can inject and make sense for troubleshooting issues.
This monitoring capabilities can be enabled on any specific IIS virtual directory by using the AppFabric configuration tool or adding the following configuration sections to your existing web app,
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<diagnostics etwProviderId="3e99c707-3503-4f33-a62d-2289dfa40d41">
<endToEndTracing propagateActivity="true" messageFlowTracing="true" />
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior name="">
<etwTracking profileName="EndToEndMonitoring Tracking Profile" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<microsoft.applicationServer>
<monitoring>
<default enabled="true" connectionStringName="ApplicationServerMonitoringConnectionString" monitoringLevel="EndToEndMonitoring" />
</monitoring>
</microsoft.applicationServer>
Bad news is that none of the configuration above can be easily set on code by using the new configuration model for WCF Web stack. A good thing is that you easily disable it in the configuration when you no longer need it, and also uses ETW, a general-purpose and high-speed tracing facility provided by the operating system (it’s part of the windows kernel).
By adding that configuration section, AppFabric will start monitoring your service automatically and providing some basic event information about the service calls. You need some custom code for injecting custom events in the monitoring data.
What I did here is to copy and refactor the “WCFUserEventProvider” class provided as sample in the Ron’s template to make it more TDD friendly when using IoC. I created a simple interface “ILogger” that any service (or resource) can use to inject custom events or monitoring information in the AppFabric database.
public interface ILogger
{
bool WriteError(string name, string format, params object[] args);
bool WriteWarning(string name, string format, params object[] args);
bool WriteInformation(string name, string format, params object[] args);
}
The “WCFUserEventProvider” class implements this interface by making possible to send the events to the AppFabric monitoring database. The service or resource implementation can receive an “ILogger” as part of the constructor.
[ServiceContract]
[Export]
public class OrderResource
{
IOrderRepository repository;
ILogger logger;
[ImportingConstructor]
public OrderResource(IOrderRepository repository, ILogger logger)
{
this.repository = repository;
this.logger = logger;
}
[WebGet(UriTemplate = "{id}")]
public Order Get(string id, HttpResponseMessage response)
{
var order = this.repository.All.FirstOrDefault(o => o.OrderId == int.Parse(id, CultureInfo.InvariantCulture));
if (order == null)
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent("Order not found");
}
this.logger.WriteInformation("Order Requested", "Order Id {0}", id);
return order;
}
}
The example above uses “MEF” as IoC for injecting a repository and the logger implementation into the service. You can also see how the logger is used to write an information event in the monitoring database.
The following image illustrates how the custom event is injected and the information becomes available for any user in the dashboard.
An issue that you might run into and I hope the WCF and AppFabric teams fixed soon is that any WCF service that uses friendly URLs with ASP.NET routing does not get listed as a available service in the WCF services tab in the AppFabric console.
The complete example is available to download from here.
© ASP.net Weblogs or respective owner