="Top">
  

Introduction

Often is the requirement in software development that the developer traces the flow of the developed application to track any bugs or issues that are raised during the testing or production run. At other times the requirement can be that the developer wants to track some mission-critical data to files or to a custom database for future use or simply they are required to audit the request responses processed by the application. The solution to that is using a good logging framework to track the data. One such well-known logging framework is log4net which helps the developers to log well-formatted events to various sinks like text files, databases etc.

What is log4net?

log4net is the MicroSoft .Net framework port of the Apache log4j framework which is used as a logging utility for Java applications. log4net allows developers to log the events to various targets like files, database stores etc.(see References section for the link to the documentation).

↑Back To Top


Scope

This article discusses the use of the log4net framework for the tracking of events in the BizTalk application. log4net can be used to track the steps of flow of the orchestration in the BizTalk application by using intelligent logging milestones in expression shapes which will let the developers know where the control is at a given point of time. These events can be logged to a text file which can be tailed using any tailing application to view the logging in the real time. This serves as an addendum to the already present debugging feature like Orchestration Debugger. log4net can be used to save the messages processed by BizTalk orchestration to an audit database if that is the business requirement(not covered in current scope as it is not an ideal way to audit messages). The sample discussed in this article will use the BizTalk Deployment Framework to accommodate and automate the deployment of the log4net configuration components as a part of the deployment process.

What is BTDF? 

BTDF is an open source deployment framework used for deploying BizTalk applications on local dev boxes as well as different environments. It provides many facilities that can be used to club together the task that require being performed pre and post-deployment of the BizTalk deployment e.g restarting of the concerned Host Instances, IIS reset etc. Another advantage of BTDF is that it is very flexible and can be configured to do a lot of tasks before, during and after the deployment of BizTalk application. All these tasks can be packaged up as a single MSI file and can be installed in the target environment. It also provides facility to define the variables related to different environments in a spreadsheet which simplifies the task of manually maintaining the binding files for the BizTalk application for multiple environments. These are some of the features of BTDF. BTDF has proven to be a very reliable tool for creating buildMSIi for BizTalk. It is a necessary weapon in the arsenal of a professional working on BizTalk platform. 
This article uses the latest version 5.7 that is released. To learn more about the installation of the btdf and step by step guide for configuring the BTDF, refer following link. The link discusses the steps taken for BizTalk 2016 and Visual Studio 2015 , but same can be followed for other versions of BizTalk.
Step by Step Guide For Installation and Configuration of BTDF for BizTalk 2016 and Visual Studio 2015  

↑Back To Top


Implementation

Following are various steps that are involved in setting up complete logging utility for a BizTalk application using log4net and BTDF.

Creating log4net  Configuration File 

In order to define what sinks that will be used to log the details from the orchestrations/helper classes/ pipeline components, the first task is to create the configuration file for the log4net . In order to use it with the BTDF framework, the file is named as <BizTalk ApplicationName>ztalk-2016-and-visual-studio-2015.aspx" target="_blank">Step by Step Guide For Installation and Configuration of BTDF for BizTalk 2016 and Visual Studio 2015  

↑Back To Top


Implementation

Following is a sample configuration file that is shown in the above screenshot. The comments in the config file explain the important concepts.

<log4net debug="true">
  <root>
    <level value="*" />
    <appender-ref ref="EventLogAppender" />
  </root>
  <!-- Logger which will call the Rolling File appender and Create the Log file
  Minimum Logging Level is DEBUG-->
  <logger name="BTDFTestProject">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender"/>
  </logger>
  <!--Rolling File Appender deined below will create a log file by Name BTDFTestLroject.log at the location configured in file
  node. appendToFile allows to add multiple log entries to the same file. maximumFileSize governs the maximum size of log file after which
  the appender will archive the current log file and create a new log file of same name-->
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file value="E:\logstore\BTDFTestProject.Log" />
    <appendToFile value="true" />
    <DatePattern value="dd-MM-yyyy" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <!--Governs the pattern in which the data will be written. The OrchestrationInstanceiD helps to correlat the
      messages in the DTA database-->
      <conversionPattern value="%date [%thread] %-5level OrchestrationInstanceId: %property{InstanceId} %logger Message: %message%newline" />
    </layout>
  </messages in the DTA database-->
      appender>
  <!--Event Log Appender is used to log events to the windows event log. This appender can be used to create custom
  applkication logs if required.-->
  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level OrchestrationInstanceId: %property{InstanceId} %logger Message: %message%newline" />
    </layout>
    <!--Defines that the logger will use the user under which the process is running
    in BizTalk terms it is either the Host Instance associated with the orchestration or the Handler processing the ports(in case of custom pipelien components)-->
    <secutrityContext type="logh4net.Util.WindowsSecurityContext">
      <credentials value="Process"></credentials>
    </secutrityContext>
    <!--Defines that the minimum level for which the entry will be logged to eventy log is Error-->
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="ERROR" />
      <levelMax value="FATAL" />
    </filter>
  </appender>
</log4net>

Configuring BTDF Project

In order to facilitate the deployment of the configuration file created above, it is necessary to configure the *.btdfproj file. Following are the settings that need to be done.

Including log4net during deploymentfilter>