A very powerful feature of BizTalk is Message Tracking. You can use the BizTalk Server Administration Console to enable message body and message property tracking. There you can also view the tracked message body and all the promoted properties for the message. There is only one but, Tracking a lot of data can be bad for the performance so Microsoft recommends that you should only enable the minimum tracking required for your application, as this will reduce the amount of data logged and lower the risk of tracking bottlenecks.

The BizTalk Performance Optimization Guide says the following about message body tracking:

Only use message body tracking if necessary. Depending on message throughput and message size, message body tracking can cause significant overhead. While BizTalk activity tracking has obvious benefits for debugging and auditing, it also has considerable performance and scalability implications. Therefore, you should track only data that is strictly necessary for debugging and security reasons, and avoids tracking redundant information.

So, it can be useful in a development- or test environment but you can’t use this feature in a production environment! But also in a Production environment you want to be able to track message bodies.

MongoDB could be a very good alternative to track message bodies because MongoDB is a web scale database, and doesn’t use SQL or JOINs, so it’s high-performance. An other advantage is that it is a document database. You can store the entire BizTalk message as a document in a collection but you can still search on specific items in the document.

 

Using MongoDB

MongoDB can be installed on many platforms such as Windows Azure but in this example I’m going to install it on the local machine.

Installing MongoDB on Windows is very simple. Download the archive, extract it and move the files to “C:\mongodb”. Also create the folder "C:\data\db". That is required to store the data files.
Now you’re good to go!
  
You can start MongoDB as a Windows Service but you can also start it from the Command Prompt: “C:\mongodb\bin\mongod.exe
 

 

Creating a Custom Itinerary Messaging Service for Message Body Tracking

In the steps below I’m going to create an Itinerary Messaging Service that you can use inside an ESB to track message bodies. Notice that you can also use the code in a “regular” Orchestration or Pipeline Component!

In a previous blog post I explained how to create a Custom Itinerary Messaging Service so in this post I’m focusing on the details!  😉
 
I’m using the officially supported C# Driver for MongoDB to insert a document in MongoDB. The C# Driver consists of two libraries: the BSON Library and the C# Driver.
  
To add the necessary libraries to a Project in Visual Studio you can use NuGet Packages.
 
Search on MongoDB and install the driver.
 
You also need the Json.NET package because you have to serialize the XML to Json. Json.NET is a popular high-performance JSON framework for .NET
 
Use JsonConvert.SerializeXmlNode to serialize a XmlDocument to Json.
 
To insert a document in a collection in MongoDB create an object representing the document and call Insert.
// This class serves as the root object for working with a MongoDB server.
MongoClient client = new MongoClient(connectionString);

var server = client.GetServer();
var mongoDbDatabase = server.GetDatabase(database);
var mongoDbCollection = mongoDbDatabase.GetCollection(collection);

// Parste the json to a BsonDocument (a collection of name/value pairs)
BsonDocument document = BsonDocument.Parse(jsonText);

// Insert the document in the collection 
WriteConcernResult insertResult = mongoDbCollection.Insert(document);

// Result
ok = insertResult.Ok;

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 
Add an entry in the itineraryServices section of the Esb.config file from the ESB Toolkit by adding an <itineraryService> element.
<itineraryService id="1030" name="TrackingService" type="itHero.ESB.Services.
TrackingService, itHero.ESB.Services, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=7f76ec21959e67b4"
scope="Messaging" stage="All"/>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

  
Now you can create an Itineray with the custom TrackingService:
  
Before you can use the Itinerary Service in BizTalk you have to register all the assemblies in the Global Assembly Cache. You can use the Deployment Framework for BizTalk but you can also use Gacutil.
  

  

Testing

Once the itinerary is deployed to the Itinerary Store database and the Tracking Service is placed in the GAC, the solution is ready to be tested.

Create a Receive Port and a Receive Location with the BizTalk Administration Console and drop a message in it.

I’ve used the Trace class in System.Diagnostics to trace the steps but you can also use another component for it like ETW tracing.

Run DebugView to watch the trace output.
 
You can use the MongoVUE application for Windows to find the messages that are stored in MongoDB.

Notice that in the Message also the InterchangeID value is available to match the Message Body with an Tracking Event in BizTalk.

 
You can match the Message Body that is stored in MongoDB with a tracked Message in BizTalk.

1. Open the BizTalk Administrator and Search For: Tracked Message Events

2. Click on an Event and use the Message ID to match the Message in the Event to the Message Body in MongoDB.

 

 

Conclusion

The Tracking Service example showed that MongoDB is a really good alternative for tracking message bodies. I’ve always been a big fan of SQL Server so I was a bit skeptical about MongoDB but seeing is believing! It really is much faster than SQL Server and you can still search on individual items in a message.

You can download the sample Messaging Service with the source code here:

http://code.msdn.microsoft.com/Using-MongoDB-for-Message-a8e4ba4d