Thursday, June 4, 2009

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:

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;

//Do the Log entry.

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)’.
//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.

No comments:

Post a Comment