Biztalk : Log entry using Microsoft Practice Enterprise Library in .Net assembly / Call .Net assembly using Biztalk Orchestration Expression Shape……

Its one of the important task in Biztalk to do the Log entry whenever you want in the orchestration. Suppose you have got an error and you need to log it before the Message going in suspended mode. One of the best solution we can use to loggin is the Microsoft Practise Enterprise library. So how will we do it? If you will implement it the first time you will face many problems like how to call .net assembly from an orchestration, configuration etc… So I thought to write an article on this full functionality. I am trying to write here as easy as possible from my side. Please let me know your comments if you have found any mistake in this article.

Important: You need to install the Microsoft Practise Enterprise Library 3.1 in your machine. You can download it from the following link:

http://www.microsoft.com/downloads/details.aspx?FamilyID=4c557c63-708f-4280-8f0c-637481c31718&displaylang=en

First you need to create a .Net class library project using VS .net 2005. Like I am creating a project named ‘TestEntLibLogging’.
Create a class named ‘EntLibraryErrorHandler.cs’ in you project.
Make it public and serializable.
Please create a method named ‘LogErrorbyEntLibrary(Exception ex)’ in the above class.

Now you need to add reference Microsoft.Practices.EnterpriseLibrary.Logging.dll in you project. To add reference in your project just right click on your project and select ‘Add Reference’. And select the ‘Microsoft.Practices.EnterpriseLibrary.Logging.dll’ from list of assembly.

Now add the following library in you class named ‘EntLibraryErrorHandler.cs’:
using Microsoft.Practices.EnterpriseLibrary.Logging;

Now in you method ‘LogErrorbyEntLibrary(Exception ex)’, write the following code to log entry:

======================================================
//Create an object of LogEntry
LogEntry objEntry = new LogEntry();

//Assign properties to LogEntry object.
objEntry.Message = ex.ToString();
objEntry.Priority = 1;
objEntry.TimeStamp = DateTime.Now;
objEntry.Categories.Add(“Exception”);

//Do the Log entry.
Logger.Write(objEntry);

========================================================

This code will work perfectly. Now we are going to implement the configuration of the config file using ‘Enterprise Library Configuration’ tool. You can find this too in the Start > Program Files > Microsoft patterns & practices > Enterprise Library 3.1 – May 2007 > Enterprise Library Configuration.

Now open you config file in this by selecting the File Menu > Open.

Now one of the question should come in your mind is which configuration file you need to configure here. Because you have not added any configuration file in you class library project. So the answer is:

You need to configure the ‘BTSNTSvc.exe.config’. You can find this file in your ‘C:\Program Files\Microsoft BizTalk Server 2006′ folder. i.e. where you have install your BizTalk.

So open this configuration file in your Enterprise Library Configuration wizard. Now right click on your config file and add new Logging Application Block by clicking on ‘New > Logging Application Block’.

There select the ‘Tract Listeners’. Right click on it and select the New > Rolling File Trace Listener.

You can select Flat File Trace Listener. Here I am creating Rolling File Trace Listener to creating the File separately for each day.

There see the properties of new added ‘Rolling Flat File Trace Listener’. Select the Formatter and select the ‘Text Formatter’ from the list.
You can update the Template properties inside the Formatter property whatever you want. To chaange the looging file’s path you have to change the property of the FileName. If you want to put the File in C:/Log/rolling.log, then you have to write it in the FileName property.

Now we have created a Rolling File Trace Listener. Now we need to assign it to ‘Formatted EventLog TraceListener’, which is in the ‘Special Sources > Logging Errors & Warnings node’. Select the ‘Formatted EventLog TraceListener’ and in the property window select the Reference Trace Listener to ‘Rolling Flat File Trace Listener’ from the dropdown. You can change the header and footer whatever you want in your logging file.

Now you can find two properties which are very important here. One is the RollFileExistsBehavior and another is RollInterval. The default value of RollFileExistsBehavior has set to OverWrite, which means it will overwrite the existing file. This we need to update to increament. For this just select the dropdown and select increament. This will create a new file based on the interval set there.

The second one is the RollInterval, which is default set to None, means it will not create new file. It will just overwrite the existing file. This we need to set depends on the requirements. I am setting it to Day, means it will daily create a new file for log entry.

Now another property ‘RollSizeKB’, This is one important property if we want to restrict to write in file more than particular size. Like I want to create the logger file not more than 5 kb size. So I will set this property. But for now I don’t require here. But if you want you can set it there.

Now you need to set the ‘Rolling Flat File Trace Listener’ in the ‘Category Sources > General > Formatted EventLog TraceListener’. Just select this node and select the Referenced TraceListener to ‘Rolling Flat File Trace Listener’ from the dropdown.

So your configuration of the config file has been completed now.

Now our remaining part is how to call the .Net assembly (that we have created the .net class library project) from Biztalk orchestration:

I am here write the following code to call the .net assembly from an Expression shape. You need to add the reference in your biztalk project. You have to right click on your biztalk project and select the Add reference. In the add reference wizard, select the Projects tab. There you will find our Class Library project. Select this project and click ok button. So now we have added the Class Library Project’s reference in our BizTalk project. Now you can access all accessible methods in your expression shape.

Write the following sentence in you catch block of the orchestration. Here I expect that you know how to use the catch exception in orchestration. Add an expression shape and write the following sentence to call the method ‘EntLibraryErrorHandler(Exception ex)’.
======================================================
TestEntLibLogging.EntLibraryErrorHandler.LogErrorbyEntLibrary(ex);
//Where ex is the Exception Object Name
======================================================

Note: Now when you run your orchestration you will face a runtime error that the EntLibraryErrorHandler not found. Its reason is that we have not add the Class Library project in GAC. To add the above file in GAC write the following command on Visual Studio Command Prompt:
======================================================
GACUTIL /i EntLibraryErrorHandler.dll
======================================================

Now test your orchestration.
I am first time writing this type of big article on my blog. So there can be some problem. And for time purpose I have not write the steps. But I think you have the initial knowledge of the Biztalk and you can understand it. Let me know if you will have any confusion or query with this article. You can add your comments here.

Advertisements

About Vijay Modi

Having 12+ years of experience in web application development. Expertise in various domains like E-Commerce, E-Learning, Insurance. I have expertise in web application development, window application development, Performance improvement, bug fixing etc. I am believing in quality work and achieving deadlines. Also like to work on new technologies and quick learner.
This entry was posted in BizTalk Server and tagged , . Bookmark the permalink.

13 Responses to Biztalk : Log entry using Microsoft Practice Enterprise Library in .Net assembly / Call .Net assembly using Biztalk Orchestration Expression Shape……

  1. Sudip says:

    I am not able to add my class library project in GAC.
    Its giving the following error :
    “Failure Adding assembly to the cache : The system cannot find the file specified.”
    Please Help me if possible.
    In case u need,
    i have named my project LoggingImplementation and the class as Logger.cs

  2. Vijay Modi says:

    have u given the strong name key to your project(LoggingImplementation). please give the strong key and test. use the following systax at your Visual Studio command prompt:

    gacutil /i yourProjectpath/bin/debug/LoggingImplementation.dll

    Hope, it will work.

    Regards,
    Vijay Modi

  3. Ashok says:

    Hi i did every thing what ever ur saying this article but the log location its not stored rolling.log file. after completion of this article i checked the BTSNTSvc.exe.config file its appear all the formats. we need to provide Trace Listner machine name…? Plz Help me…….

  4. Vijay Modi says:

    Ashok,

    can you please check the event viewer and let me know whats the error are you getting when you run your applicatin.

    Thanks,
    Vijay Modi

  5. jc says:

    You need to strong name the assembly.

    cheers.

  6. Yadnesh says:

    Good Post.

    I am new to enterrpise library.

    How can i strong name enterprise library 3.1 assemblies?

    Reply back as soon as possible. I am into middle of project critical stage.

    Appriciate your help.!!
    Thanks!!

  7. Vijay Modi says:

    You can generate the Strong Name key using
    sn.exe -k testKey.snk
    command and you need to run it in Visial Studio Command prompt.

    But for enterprise library you donot need to assign Strong Name key. You can just deply it in Global Assembly Catch and it will resolve your problem.

    Let me know if you will have more queries.

    Regards,
    Vijay Modi

  8. kiran says:

    we are facing issue running multiple versions of enterprise library, have you ever ran multiple enterprise versions parallely?

  9. Vijay,

    Thanks for this post. I’ve been meaning for some time to look into Enterprise Library logging for BizTalk, and you just saved me some time. Thanks.

    One question: is it possible to have different orchestrations log to different log files? I haven’t yet tried running the steps in your post, but it would seem all orchestrations would log to the same place since they use the same configuration file, btsntsvc.exe.cfg. Is there a way to segregate this?

    • Vijay Modi says:

      Hi Victor,

      Yes, our orchestration can log in different log file. For this we can call a .Net Assembly (.Net Class method). So we will create a .Net class library project and we will create a class to log errors. Suppose we have created a method named LogErrors(String Error) and we will call this method from our orchestration. And based on orchestration request we can write code in our .Net class assembly to log error different places.

      LogEntry objLogEntry = new LogEntry();

      objLogEntry .Message = error.ToString();
      objLogEntry .MachineName = System.Environment.MachineName;
      objLogEntry .TimeStamp = System.DateTime.Now;
      objLogEntry .Categories.Add(“Testing Roll Listener”);

      Logger.Write(objEntry);

      Thanks for your comments. Let me know if you will have any query regarding this.

  10. Vijay,

    Thanks for the reply. I don’t quite understand. The log file is set in the btsntsvc.exe.cfg file, so how can the log file location be dynamically changed in the .Net assembly? I didn’t see any FileName attributes in the LogEntry class. Can you provide a short code sample?

  11. Rethi says:

    Hi Vijay

    I used to read your posts. i felt it as really useful… so i thought of posting this error to you for getting the respose as early as possible.

    I m getting the following error while trying to execute logging from biztalk

    Description:
    Exception Occured in LoggingInvalid TraceListenerData type in configuration ‘listenerDataType=”Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging”‘. (D:\Program Files\BizTalk Server\BTSNTSvc.exe.Config line 31)

    I m using Rolling flat file listner, flat file and event log. If i use rolling flat file alone we ae getting response. But Flat file and Event log is creating problem. I know it s some problemw ith dll or strong name.

    Please find the configuration in Btsntsvc.exe

    loggingConfiguration name=”Logging Application Block” tracingEnabled=”true” defaultCategory=”General”>

    please let me know if anything wring with this as early as possible.

    Thanks in Advance

    Rethi

    • Rethi says:

      vijay,

      Configuration which is given in previous post has not come it seems. So please find the same
      configSections>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s