BizTalk: Log HL7 Parsing Errors

HL7 Disassembler writes parsing errors in windows event log and there is no documented way to get those error through code to log in custom database or some other data store.

Here is what HL7 pipeline writes into event log:

SNAGHTML1c204d9

we can get these error in code by writing a wrapper disassembler component on top of HL7 OOTB Disassembler

Omitting details about how to write a custom pipeline component, we just need to override Disassemble and GetNext methods

  public HL7DASM()
        {
            HL7Dasm = new HL72fDasm();
        }
      public new void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            HL7Dasm.Disassemble(pContext, pInMsg);
        }

If HL7 party is configured to return ACKs, GetNext() method will execute multiple times.

public new IBaseMessage GetNext(IPipelineContext pContext)
        {
            IBaseMessage msg = HL7Dasm.GetNext(pContext);

            if (msg != null && !msg.BodyPart.Data.CanSeek)  //if parsing fails, stream is not seekable
            {

                 FieldInfo ParserErrorField = HL7Dasm.GetType().GetField("mBodyErrors", BindingFlags.NonPublic | BindingFlags.Instance); 
var ParserError = (System.Collections.ArrayList)ParserErrorField.GetValue(HL7Dasm);
              
StringBuilder errorList = new StringBuilder();

                foreach (HL7Error error in ParserError)
                   {
                     errorList.AppendLine(error.ToString());
                   }
    // now errorList string contains all parsing errors details in same format as in windows event log
                }
              }
            return msg;
        }