Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / biztalk

Tip 2 - Using Nlog in Biztalk Orchestration

5.00/5 (4 votes)
28 Dec 2015CPOL1 min read 20.4K   70  
Use NLog to log messages in biztalk orchestration and save logs to log file

Introduction

This tip provides a simple explanation of how to use NLog for log messages inside biztalk orchestration.

The tip is divided into 4 parts:

  1. Install NLog for C# application
  2. Update Biztalk Configuration
  3. Development
  4. Configure biztalk administration console [ run sample - get output ]

Getting Started

Part 1: Install NLog for C# Application

  1. Create new C# application "HelperLibrary"
  2. Open package manager console:

  3. Write command: Install-Package NLog.Config

  4. This will install NLog for the current application.

  5. Edit on NLog.Config.
    XML
    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!-- 
      See https://github.com/nlog/nlog/wiki/Configuration-file 
      for information on customizing logging rules and outputs.
       -->
     <targets>
        <!-- local file logging in C:\ Partition-->
        <target name="TraceFile" xsi:type="File"
                fileName="C:\logs\BiztalkTraceLogs.log"
                layout="
    -------------- ${level} (${longdate}) --------------${newline}
    ${newline}
    Additional Info: ${message}${newline}" />
        
        <target name="ErrorFile" xsi:type="File"
                fileName="C:\logs\BiztalkErrorLogs.log"
                layout="
    -------------- ${level} (${longdate}) --------------${newline}
    ${newline}
    Exception Type: ${exception:format=Type}${newline}
    Exception Message: ${exception:format=Message}${newline}
    Stack Trace: ${exception:format=StackTrace}${newline}
    Additional Info: ${message}${newline}" />
      </targets>
     <rules>
        <logger name="*" level="Trace" writeTo="TraceFile" />
        <logger name="*" levels="Debug,Error" writeTo="ErrorFile" />
      </rules>
    </nlog>

Part 2: Update Biztalk Server Configuration

  1. Navigate to biztalk folder location in program files

    Program Files (x86)Microsoft BizTalk Server 2013 R2

  2. Open BTSNTSvc64.exe or BTSNTSvc.exe according to version of biztalk and update file
    XML
    <?xml version="1.0" ?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0" />
        </startup>
    <!-- 1. add config Section for NLog -->
      <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
      </configSections>
        <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <probing privatePath="BizTalk Assemblies;Developer Tools;Tracking;Tracking\interop" />
            </assemblyBinding>
        </runtime>
        
        <system.runtime.remoting>
        
            <channelSinkProviders>
                <serverProviders>
                    <provider id="sspi" 
                    type="Microsoft.BizTalk.XLANGs.BTXEngine.
                    SecurityServerChannelSinkProvider,Microsoft.XLANGs.BizTalk.Engine" 
                    securityPackage="ntlm" authenticationLevel="packetPrivacy" />
                </serverProviders>
            </channelSinkProviders>
        
            <application>
                <channels>
                    <channel ref="tcp" port="0" name="">
                        <serverProviders>
                            <provider ref="sspi" />
                            <formatter ref="binary" typeFilterLevel="Full"/>
                        </serverProviders>
                    </channel>
                </channels>
            </application>
        </system.runtime.remoting>
    <!-- 2. Call NLog.config -->
          <nlog file=" NLog.config" />
    </configuration>
  3. Copy NLog.config file to biztalk server location

    Program Files (x86)Microsoft BizTalk Server 2013 R2

Part 3: Development

  1. Add NLogManager.cs Class to HelperLibrary C# application:
    C#
    using NLog;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace HRManagement.HelperLibrary
    {
     [Serializable()]
        public static class NLogManager
        {
     private static readonly Logger logman = LogManager.GetCurrentClassLogger();
         public static void Log(LogType level, string message, Exception exception)
    {
                switch (level)
                {s
         case LogType.Error:
                        logman.Log(LogLevel.Error, exception , message , null);
                        break;
                    case LogType.Info:
                        logman.Log(LogLevel.Info, exception, message, null);
                        break;
                    case LogType.Warning:
                        logman.Log(LogLevel.Warn, exception, message, null);
                        break;
                    case LogType.Trace:
                        logman.Log(LogLevel.Trace, exception, message, null);
                        break;
                }
                if (exception.InnerException != null)
      
                {
                    Log(level, message, exception.InnerException);
                }
            }
            public static void Log(LogType level, string message)
            {
                switch (level)
                {
                    case LogType.Error:
                        logman.Log(LogLevel.Error, message);
                        break;
                    case LogType.Info:
                        logman.Log(LogLevel.Info, message);
     break;
                    case LogType.Warning:
                        logman.Log(LogLevel.Warn, message);
                        break;
                    case LogType.Trace:
                        logman.Log(LogLevel.Trace, message);
                        break;
                }  }
        } 
    
    [Serializable()]
        public enum LogType
        {
            Error, Info, Warning, Trace
        }
    }
  2. Build HelperLibrary and deploy to GAC, deploy helper to gac like Tip1-Add_Dll_To_Gac.

  3. Add biztalk application + add reference for helperLibrary.

  4. Create new sample schema to route using orchestration [EmployeeSchema]

  5. Create your sample orcestration [simple for receive - write log - send]

  6. Call HelperLibrary to use NLogManager Class to start log and specify LogType [Sample Trace]

  7. Deploy Biztalk Application [add strong key + deploy]

     

     

Part 4: Configure biztalk Administration Console [Run Sample - Get Output]

  1. Bound orchestration, create physical receive, send ports
  2. Add resources of nlog, helper DLL

  3. Route messages [Employee Schema] from receive to send port
  4. Get Results in sendPort, you find the log [BiztalkTraceLogs.txt] in folder specified in NLog.Config [c:\logs\]

  5. Finally, you can add more logs for any shape in orchestration, and enjoy logs types and collect all logs into one file or multiple according to configuration in NLog.config.

License

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