Click here to Skip to main content
13,772,340 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 11 Apr 2016
Licenced CPOL

AzureBizTalkServices Custom Logging And Exception Handling in Custom Inspector Code

, 11 Apr 2016
Rate this:
Please Sign up or sign in to vote.
How to do logging & exception handling in custom inspector code in Azure biztalk services

Editorial Note

This article is an entry in our Windows Azure Developer Challenge. Articles in this sub-section are not required to be full articles so care should be taken when voting. Create your free Azure Trial Account to Enter the Challenge.


This article explains how to do logging & exception handling in custom inspector code in Azure biztalk services.


The basic idea is, Azure BizTalk Bridge service receives XML message and we can log the received message at validate stage. Below is the custom inspector code to log the received message in Azure storage account. To handle exceptions, we can use namespace System.ServiceModel; Here, I am logging the message in Azure Storage account.

Using the Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.BizTalk.Services;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.ServiceModel;

// Using this code we can log the incoming message data
namespace CustomeInspectorCode.Inspectors
    class CustomLogging : IMessageInspector
        public Task Execute(IMessage message, IMessageInspectorContext context)
            return Task.Factory.StartNew(() =>
                //This can write below line in azure storage account WADSLog Table
                context.Tracer.TraceEvent(TraceEventType.Information, "LOGGING INSPECTOR STARTED");

                String expMessage = string.Empty;                

                    //Converting Incoming IMessage to String type
                    string msgString;

                    using (MemoryStream memStrm = new MemoryStream())
                        message.Data.Position = 0;
                        memStrm.Position -= memStrm.Position;

                        using (StreamReader strReader = new StreamReader(memStrm, Encoding.UTF8))
                            msgString = strReader.ReadToEnd();

                    if (msgString.IndexOf('<') > 0)
                        msgString = msgString.Substring(msgString.IndexOf('<'));

                    StringBuilder msgStringBuilder = new StringBuilder();
                    char ch;
                    //To remove special characters
                    for (int i = 0; i < msgString.Length; i++)
                        ch = msgString[i];
                        if ((ch == 0x9) || ch == '\t' || ch == '\n' || 
                        ch == '\r' || (ch == 0xA) || (ch == 0xD) || 
                        ((ch >= 0x20) && (ch <= 0xD7FF)) || 
                        ((ch >= 0xE000) && (ch <= 0xFFFD)) || 
                        ((ch >= 0x10000) && (ch <= 0x10FFFF)))
                    String logString = " MESSAGE BODY: " + msgStringBuilder.ToString();
                    //write the received message (converted to string) in storage account
                    context.Tracer.TraceEvent(TraceEventType.Information, logString);

                //handle the exceptions by using FaultException 
                catch (Exception ex)
                    "LOGGING INSPECTOR FAILURE. DETAILS: " + ex.Message);
                    throw new FaultException("EXCEPTION OCCURED IN CUSTOM LOGGING. 
                    DETAILS:"+ex.Message, new FaultCode("5001")); 
                context.Tracer.TraceEvent(TraceEventType.Information, "LOGGING INSPECTOR ENDED");

Points of Interest

You can find the logged message in WADSLog Table in Azure storage account. In case of any exceptions that occurred during inspector code execution, the bridge service will throw the error details in response envelope like below. Here, you can find the fault code and details:

<s:Envelope xmlns:s="" 

    <a:Action s:mustUnderstand="1"

    <ActivityId CorrelationId="xxxxxxxxxxxxxx" 

        <s:Text xml:lang="en-US">


  • 11th April, 2016: Initial version


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


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

GeneralDeployment Pin
Member 1208730517-Aug-16 2:02
memberMember 1208730517-Aug-16 2:02 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181119.1 | Last Updated 11 Apr 2016
Article Copyright 2016 by Hari Krishna Poluru
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid