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

Stats

2.7K views
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.

Introduction

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

Background

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;                

                try 
                {
                    //Converting Incoming IMessage to String type
                    string msgString;

                    using (MemoryStream memStrm = new MemoryStream())
                    {
                        message.Data.CopyTo(memStrm);
                        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)))
                        {
                            msgStringBuilder.Append(ch);
                        }
                    }
                    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)
                {
                    context.Tracer.TraceEvent(TraceEventType.Error, 
                    "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="http://www.w3.org/2003/05/soap-envelope" 

xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1"

    >http://www.w3.org/2005/08/addressing/soap/fault</a:Action>
    <a:RelatesTo>xxxxxxxxxxxxxxxxxxxxxxxxxxxx</a:RelatesTo>
    <ActivityId CorrelationId="xxxxxxxxxxxxxx" 

    xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">xxxxxxxxxxxxxxxxxxxxxx</ActivityId>
  </s:Header>
  <s:Body>
    <s:Fault>
      <s:Code>
        <s:Value>s:5001</s:Value>
      </s:Code>
      <s:Reason>
        <s:Text xml:lang="en-US">
        EXCEPTION OCCURED IN CUSTOM LOGGING. DETAILS:Error Details </s:Text>
      </s:Reason>
    </s:Fault>
  </s:Body>
</s:Envelope>

History

  • 11th April, 2016: Initial version

License

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

Share

About the Author

No Biography provided

You may also be interested in...

Pro

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